android数据库错误
单击一个Button,创建数据库并插入数据,onclick方法调用下面两个函数。但是有错privatevoidCreateTable(){SQLiteDatabasedb...
单击一个Button,创建数据库并插入数据,onclick方法调用下面两个函数。但是有错
private void CreateTable() {
SQLiteDatabase db = lgDbhlper.getWritableDatabase();
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
Log.i("gx:createDB=", sql);
try {
db.execSQL("DROP TABLE IF EXISTS diary"); //如果存在,删除原先表
db.execSQL(sql);
} catch (SQLException e) {
}
}
private long insertItem(String name, String password) {
SQLiteDatabase db = lgDbhlper.getWritableDatabase();
ContentValues insertValues = new ContentValues();
insertValues.put(KEY_TITLE, name);
insertValues.put(KRY_PASSWORD, password);
return db.insert(DATABASE_NAME, null, insertValues);
}
logcat 信息
11-27 11:51:38.480: I/gx:createDB=(227): CREATE TABLE log_in (name text not null, password text not null );
11-27 11:51:38.480: E/Database(227): Failure 1 (table log_in already exists) on 0x390eb0 when preparing 'CREATE TABLE log_in (name text not null, password text not null );'.
11-27 11:51:38.520: E/Database(227): Error inserting =
11-27 11:51:38.520: E/Database(227): android.database.sqlite.SQLiteException: near "in": syntax error: , while compiling: INSERT INTO log in() VALUES(?);
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1027)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1413)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1286)
11-27 11:51:38.520: E/Database(227): at com.androidbook.regist_window.insertItem(regist_window.java:119)
11-27 11:51:38.520: E/Database(227): at com.androidbook.regist_window.access$1(regist_window.java:113)
11-27 11:51:38.520: E/Database(227): at com.androidbook.regist_window$2.onClick(regist_window.java:51)
...
请问各位大神是哪里错了。。。 展开
private void CreateTable() {
SQLiteDatabase db = lgDbhlper.getWritableDatabase();
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
Log.i("gx:createDB=", sql);
try {
db.execSQL("DROP TABLE IF EXISTS diary"); //如果存在,删除原先表
db.execSQL(sql);
} catch (SQLException e) {
}
}
private long insertItem(String name, String password) {
SQLiteDatabase db = lgDbhlper.getWritableDatabase();
ContentValues insertValues = new ContentValues();
insertValues.put(KEY_TITLE, name);
insertValues.put(KRY_PASSWORD, password);
return db.insert(DATABASE_NAME, null, insertValues);
}
logcat 信息
11-27 11:51:38.480: I/gx:createDB=(227): CREATE TABLE log_in (name text not null, password text not null );
11-27 11:51:38.480: E/Database(227): Failure 1 (table log_in already exists) on 0x390eb0 when preparing 'CREATE TABLE log_in (name text not null, password text not null );'.
11-27 11:51:38.520: E/Database(227): Error inserting =
11-27 11:51:38.520: E/Database(227): android.database.sqlite.SQLiteException: near "in": syntax error: , while compiling: INSERT INTO log in() VALUES(?);
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1027)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1413)
11-27 11:51:38.520: E/Database(227): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1286)
11-27 11:51:38.520: E/Database(227): at com.androidbook.regist_window.insertItem(regist_window.java:119)
11-27 11:51:38.520: E/Database(227): at com.androidbook.regist_window.access$1(regist_window.java:113)
11-27 11:51:38.520: E/Database(227): at com.androidbook.regist_window$2.onClick(regist_window.java:51)
...
请问各位大神是哪里错了。。。 展开
3个回答
展开全部
看楼主的日志,应该是表重复创建了( Failure 1 (table log_in already exists) )。
楼主要创建的表名叫log_in吧,你在创建表之前想用一句语句判断是否该表已存在,如果存在就删除:
db.execSQL("DROP TABLE IF EXISTS diary"); //如果存在,删除原先表
可你这里判断存在的表写死了diary,而不是你要创建的log_in,所以在你创建log_in的时候重复了。
下面还有句日志(near "in": syntax error: , while compiling: INSERT INTO log in() VALUES(?);
)显示你给的表名是log in,而不是log_in,导致语法错误了
楼主要创建的表名叫log_in吧,你在创建表之前想用一句语句判断是否该表已存在,如果存在就删除:
db.execSQL("DROP TABLE IF EXISTS diary"); //如果存在,删除原先表
可你这里判断存在的表写死了diary,而不是你要创建的log_in,所以在你创建log_in的时候重复了。
下面还有句日志(near "in": syntax error: , while compiling: INSERT INTO log in() VALUES(?);
)显示你给的表名是log in,而不是log_in,导致语法错误了
更多追问追答
追问
那这个重复创建该怎么改啊?谢谢啦
追答
db.execSQL("DROP TABLE IF EXISTS diary"); //如果存在,删除原先表
改成
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); //如果存在,删除原先表
展开全部
try {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); //如果存在,删除原先表
db.execSQL(sql);
} catch (SQLException e) {
}
这样写应该没问题了吧?应该是你写的删除表格的语句中表名写错了而已。
Oh,没看见,下面已经有人回答了,当我没说,呵呵
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); //如果存在,删除原先表
db.execSQL(sql);
} catch (SQLException e) {
}
这样写应该没问题了吧?应该是你写的删除表格的语句中表名写错了而已。
Oh,没看见,下面已经有人回答了,当我没说,呵呵
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
SQL语句末尾多了一个分号,改成下面就可以了:
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ")";
+ " text not null, " + BODY + " text not null " + ");";
SQL语句末尾多了一个分号,改成下面就可以了:
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ")";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询