我是android的新手,正在创建第一个创建数据库的应用程序。我想在单击保存按钮时将数据添加到我的数据库。但是我收到了一个错误。
我的logcat说
"12-09 12:15:41.204: ERROR/Database(565): Failure 1 (near "AUTO": syntax error) on 0x12f148 when preparing 'CREATE TABLE GoalTable (_id INTEGER PRIMARY KEY AUTO INCREMENT, goals_title TEXT NOT NULL, start_date TEXT NOT NULL, target_date TEXT NOT NULL, reminder_date TEXT NOT NULL,);'.".
请帮我找错。感谢。
Database.java
package com.firstproject.aditya;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
public class Database {
public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "goals_title";
public static final String KEY_STDATE = "start_date";
public static final String KEY_TGDATE = "target_date";
public static final String KEY_RDDATE = "reminder_date";
private static final String DATABASE_NAME = "GoalInformation";
private static final String DATABASE_TABLE = "GoalTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private android.database.sqlite.SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(android.database.sqlite.SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTO INCREMENT, " + KEY_TITLE
+ " TEXT NOT NULL, " + KEY_STDATE + " TEXT NOT NULL, "
+ KEY_TGDATE + " TEXT NOT NULL, " + KEY_RDDATE
+ " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(android.database.sqlite.SQLiteDatabase db,
int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Database(Context c) {
ourContext = c;
}
public Database open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String gTitle, String dateS, String dateT,
String dateR) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_TITLE, gTitle);
cv.put(KEY_STDATE, dateS);
cv.put(KEY_TGDATE, dateT);
cv.put(KEY_RDDATE, dateR);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_TITLE, KEY_STDATE,
KEY_TGDATE, KEY_RDDATE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iTitle = c.getColumnIndex(KEY_TITLE);
int iStdate = c.getColumnIndex(KEY_STDATE);
int iTgdate = c.getColumnIndex(KEY_TGDATE);
int iRddate = c.getColumnIndex(KEY_RDDATE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + " " + c.getString(iTitle)
+ " " + c.getString(iStdate) + " " + c.getString(iTgdate)
+ " " + c.getString(iRddate) + "\n";
}
return result;
}
}
答案 0 :(得分:4)
AUTO
和INCREMENT
之间不应有空格。它应该是AUTOINCREMENT
答案 1 :(得分:1)
AUTO INCREMENT
之间不应有空格,应为AUTOINCREMENT
答案 2 :(得分:0)
onCreate语句中存在语法错误。查看语句的最后一部分,将“符号放在里面”,即+“TEXT NOT NULL”);