2011-11-24 131 views
2

我得到一个错误我找不出来。希望你能帮助!Android SQLite插入错误

我正在制作一个具有唯一标识的组名表,这是主键。

public class GroupDatabaseAdapter { 

public static final String KEY_ID = "id"; 
public static final String KEY_NAME = "name"; 
private static final String DATABASE_TABLE = "groep"; 
private Context context; 
private SQLiteDatabase database; 
private GroupDatabaseHandler dbHandler; 

public GroupDatabaseAdapter(Context context) { 
    this.context = context; 
} 

public GroupDatabaseAdapter open() throws SQLException { 
    dbHandler = new GroupDatabaseHandler(context); 
    database = dbHandler.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    dbHandler.close(); 
} 

/** 
* Create a new group If the group is successfully created return the new 
* rowId for that note, otherwise return a -1 to indicate failure. 
*/ 
public long createGroup(long id, String name) 
{ 
    ContentValues values = createContentValues(id, name); 

    return database.insert(DATABASE_TABLE, null, values); 
} 
/** 
* Return a Cursor over the list of all groups in the database 
* 
* @return Cursor over all groups 
*/ 
public Cursor fetchAllGroups() { 
    return database.query(DATABASE_TABLE, new String[] { KEY_ID, 
      KEY_NAME }, null, null, null, 
      null, null); 
} 

private ContentValues createContentValues(Long id, String name) { 
    ContentValues values = new ContentValues(); 
    values.put(KEY_ID, id); 
    values.put(KEY_NAME, name); 
    return values; 
} 
} 



public class GroupDatabaseHandler extends SQLiteOpenHelper { 

private static final String DATABASE_NAME = "groups.db"; 

private static final int DATABASE_VERSION = 1; 

// Database creation sql statement 
private static final String DATABASE_CREATE = "CREATE TABLE lesson (id INTEGER PRIMARY KEY NOT NULL , name TEXT NOT NULL);"; 

public GroupDatabaseHandler(Context context) 
{ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase database) 
{ 
    database.execSQL(DATABASE_CREATE); 
} 

} 

我从另一个调用类的这些类与下面的代码:

GroupDatabaseAdapter groupDb = new GroupDatabaseAdapter(this).open(); 
long id = groupDb.createGroup(641, "3 bac group 2"); 
System.out.println(id); 
id = groupDb.createGroup(642, "3 bac group 3"); 
System.out.println(id); 

我得到的错误是

11-24 23:08:07.036: I/Database(11436): sqlite returned: error code = 1, msg = near "group": syntax error 
11-24 23:08:07.075: E/Database(11436): Error inserting id=1 name=3 bac group 2 
11-24 23:08:07.075: E/Database(11436): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: INSERT INTO group(id, name) VALUES(?, ?); 

在同一个节目我做了另一个表,不工作。该表与此表之间唯一的区别在于主键。在另一个表中它是自动递增的,在这里我想让rowid成为组的id。 希望有人看到我的错误。

好吧,我改组为GROEP(荷兰语为组),现在我得到这些错误:

11-25 00:09:10.812: I/Database(457): sqlite returned: error code = 1, msg = no such table: groep 
11-25 00:09:11.082: E/Database(457): Error inserting _id=1 name=3 bac group 2 
11-25 00:09:11.082: E/Database(457): android.database.sqlite.SQLiteException: no such table: groep: , while compiling: INSERT INTO groep(_id, name) VALUES(?, ?); 

干杯! Kat

+0

我只是告诉你,对于数据库表的主键,您应该始终使用标识符“_id”,因为某些Android功能依赖于此标准。在你的查询中,你总是可以说“ID为_id”,但仍然... – hovanessyan

回答

0

您创建了一个名为lesson的表并将其插入名为groep的表中。

+0

哦,那太愚蠢了!从我自己的代码复制粘贴。 谢谢!它正在工作! – Kat

5

只是一种预感:尝试将数据库表重命名为“group”(例如“groups”)以外的内容。我很确定“group”是SQL中的一个保留关键字。

+0

我调整了代码,现在我又得到了一个错误。我编辑了我的问题。 – Kat

+0

对,这是因为'onCreate(SQLiteDatabase数据库)'没有运行。转到应用程序(在手机/模拟器下的设置)和**卸载**你的应用程序。这将删除以前版本的数据库。 – dmon

+0

我只是运行它的android模拟器。我已经认为我必须重置程序或数据。但我找不到如何。我试图删除数据库文件,但它仍然无法正常工作。 – Kat