public ArrayList< String > query(int id){
help helper=new help(Main2Activity.this);
ArrayList< String >list=new ArrayList< >();
SQLiteDatabase db=helper.getReadableDatabase();
Cursor c=db.rawQuery("select * from user where id=?",new String[]{id+""});
if (c.moveToFirst()) {
do{
list.add(c.getString(1));
}while (c.moveToNext());
}
db.close();
return list;
}
提示报错
E/SQLiteLog: (1) near "=": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.kaoqin, PID: 2593
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.kaoqin/com.example.dell.kaoqin.Main2Activity}: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: select * from user where id like=?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: select * from user where id like=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.< init >(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.< init >(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.dell.kaoqin.Main2Activity.query(Main2Activity.java:63)
at com.example.dell.kaoqin.Main2Activity.onCreate(Main2Activity.java:32)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
声明数据库代码
db.execSQL("create table denglu(id int,password)");
db.execSQL("create table user(id,day,kecheng,kaoqin)");
Log.i("test","声明数据库");
但是我把代码改成 Cursor c=db.rawQuery("select * from user where id like ?",new String[]{id+""});就可以正常查询,实在搞不懂哪里错了
回复讨论(解决方案)
sql语句的语法是:select * from user where id = ? 或者select * from user where id like ?
select * from user where id like=? 不行
sql语句的语法是:select * from user where id = ? 或者select * from user where id like ?
select * from user where id like=? 不行
但是我没有like=?啊,一个是id like ?一个是id=?
但是是否和我新建表时没有设置id的类型有关呢? create table denglu(id int,password) 这肯定不行呀,要指定字段的类型
create table denglu(id int,password) 这肯定不行呀,要指定字段的类型
但是我另一个查询第一个表denglu的方法用的是
Cursor c=db.rawQuery("select * from denglu where id=?and password=?",new String[]{id+"",password});
但是却可以查询成功,我password也没有设置类型啊?
create table denglu(id int,password) 这肯定不行呀,要指定字段的类型
我password传入的是字符串类型的