我为我的 Android 手机制作了一个电子邮件客户端,现在我想将电子邮件保存在 SQLite 数据库中。但是如果我只想用测试数据填充数据库,我会出现这个错误:
E/SQLiteLog(21212): (1) "TO" 附近: 语法错误
E/SQLiteDatabase(21212): 插入错误 SUBJECT=test subject TO=emailadress2 FROM=emailadress1 CONTENT=test content
android.database.sqlite.SQLiteException:靠近“TO”:语法错误(代码 1):,编译时:INSERT INTO MAIL(SUBJECT,TO,FROM,CONTENT) VALUES (?,?,?,?)
(...)
(1) “FROM”附近:语法错误
E/错误(21212):android.database.sqlite.SQLiteException:靠近“FROM”:语法错误(代码 1):,编译时:SELECT ID、FROM、TO、SUBJECT、CONTENT FROM MAIL WHERE ID = -1
我对数据库使用以下代码:
MAINACTIVITY.java
public void dbCreate(View view){
String from = "emailadress1";
String to = "emailadress2";
String subject = "test subject";
String content = "test content";
try {
datasource.open();
datasource.createEntry(from, to, subject, content);
datasource.close();
}
catch (Exception ex) {
Toast.makeText(this,"Error", Toast.LENGTH_LONG).show();
Log.e("Error", ex.toString());
}
}
MYSQLiteHelper.java
private static final String DATABASE_NAME = "mail.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_CREATE_MAIL = "CREATE TABLE MAILS ( ID integer PRIMARY KEY AUTOINCREMENT, FROM TEXT, TO TEXT, SUBJECT TEXT, CONTENT TEXT);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
Log.i("Info", "OnCreate aufgerufen");
database.execSQL(TABLE_CREATE_MAIL);
}
数据源.java
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { "ID", "FROM",
"TO", "SUBJECT", "CONTENT"};
public Entry createEntry(String from, String to, String subject, String content) {
ContentValues values = new ContentValues();
values.put("FROM", from);
values.put("TO", to);
values.put("SUBJECT", subject);
values.put("CONTENT", content);
long insertId = database.insert("MAIL", null,
values);
Cursor cursor = database.query("MAIL",allColumns, "ID = " + insertId, null, null, null, null, null);
cursor.moveToFirst();
return cursorToEntry(cursor);
}