我正在使用数据库来存储数据库中的组详细信息和联系信息,但每当我将一些数据插入数据库时,我会收到错误,说“没有这样的表”。我做错了什么?
代码
public class GroupDataBase extends SQLiteOpenHelper {
private static final int dbVersion = 1;
private static final String dbName = "HSsuraksha";
private static final String tableName = "groupDetails";
private static final String groupId = "groupId";
private static final String groupName = "groupName";
private static final String createdOn = "createdOn";
private static final String createTable = "CREATE TABLE " + tableName + "(" + groupId + " Integer Primary Key Auto Increment," + groupName + " Text," + createdOn + " Text" + ")";
public GroupDataBase(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public void insertGroupDetails(GroupModel groupModel) {
SQLiteDatabase database = getWritableDatabase();
database.beginTransaction();
ContentValues contentValues = new ContentValues();
contentValues.put(groupName, groupModel.getGroupName());
contentValues.put(createdOn, groupModel.getGroupCreatedDate());
if (contentValues != null) {
Long id = database.insert(tableName, null, contentValues);
Log.e("Group insert values", "" + id);
}
database.setTransactionSuccessful();
database.endTransaction();
database.close();
}
}
logcat的
08-01 12:57:26.129 2522-2522/example.com.pocketdocs E/SQLiteLog﹕ (1) no such table: groupDetails
08-01 12:57:26.129 2522-2522/example.com.pocketdocs E/SQLiteDatabase﹕ Error inserting createdOn=2014-08-01 groupName=test
android.database.sqlite.SQLiteException: no such table: groupDetails (code 1): , while compiling: INSERT INTO groupDetails(createdOn,groupName) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1475)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1347)
at example.com.pocketdocs.DataBase.GroupDataBase.insertGroupDetails(GroupDataBase.java:50)
at example.com.pocketdocs.Group.CreateNewGroup.onClick(CreateNewGroup.java:87)
at android.view.View.performClick(View.java:4147)
at android.view.View$PerformClick.run(View.java:17161)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4787)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
08-01 12:57:26.129 2522-2522/example.com.pocketdocs E/Group insert values﹕ -1
我是否写过创建表查询错误????
答案 0 :(得分:1)
您的CREATE TABLE
语法错误:auto
和increment
之间不应有空格。
由于您没有看到有关它的例外情况,因此您的onCreate()
尚未以当前形式运行。
修复语法:使用autoincrement
。
卸载您的应用,以便重新创建数据库。有关详情,请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?。
答案 1 :(得分:1)
而不是您的表创建查询使用以下
private static final String createTable = "CREATE TABLE " + tableName + "(" + groupId + " Integer Primary Key Autoincrement," + groupName + " Text," + createdOn + " Text" + ")";
我认为“自动增量”中的问题应该在一起。
答案 2 :(得分:0)
将您的行更改为此
private static final String createTable = "CREATE TABLE " + tableName + "(" + groupId + " Integer Primary Key Autoincrement," + groupName + " Text," + createdOn + " Text" + ");";