我正在使用 Android 制作时间表应用程序。我是初学者,对数据库编程知之甚少。
下面是我正在使用的数据库的代码:
public class TsData{
private static final String TS_DATABASE = "timesheetdb";
private static final int DATABASE_VERSION = 1;
private static final String TS_TABLE = "database";
public static final String ROW_ID = BaseColumns._ID;
public static final String PROJ_NAME = "projname";
public static final String START_TIME = "stime";
public static final String DATE = "date";
public static final String STOP_TIME="";
public static final String ACT_NAME = "actname";
public static final String TOTAL_TIME = "ttime";
private DBhelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDataBase;
private static class DBhelper extends SQLiteOpenHelper{
public DBhelper(Context context) {
super(context, TS_DATABASE, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TS_TABLE + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ACT_NAME + " TEXT NOT NULL, "
+ DATE + " TEXT NOT NULL, "
+ START_TIME + " TEXT NOT NULL, "
+ STOP_TIME + " TEXT NOT NULL" + ");";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TS_TABLE);
onCreate(db);
}
}
public TsData(Context c){
ourContext=c;
}
public TsData open()throws SQLException{
ourHelper = new DBhelper(ourContext);
ourDataBase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long insertEntry(String act_name2, String startDate, String startTime, String stopTime) {
ContentValues values = new ContentValues();
values.put(ACT_NAME, act_name2);
values.put(DATE, startDate);
values.put(START_TIME, startTime);
values.put(STOP_TIME, stopTime);
System.out.println("record inserted");
return ourDataBase.insert(TS_TABLE, null, values);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ROW_ID, ACT_NAME, DATE, START_TIME, STOP_TIME};
Cursor cr = ourDataBase.query(TS_TABLE, columns, null, null, null, null, null);
String result="";
int iRow=cr.getColumnIndex(ROW_ID);
int iName=cr.getColumnIndex(ACT_NAME);
int istartTime=cr.getColumnIndex(START_TIME);
int istopTime=cr.getColumnIndex(STOP_TIME);
int iDate=cr.getColumnIndex(DATE);
for(cr.moveToFirst();!cr.isAfterLast();cr.moveToNext()) {
result=result+cr.getString(iRow)+" "
+cr.getString(iName)+" "
+cr.getString(iDate)+" "
+cr.getString(istartTime)+" "
+cr.getString(istopTime)+"\n";
}
return result;
}
public void deleteEntry(long rowNo) {
// TODO Auto-generated method stub
ourDataBase.delete(TS_TABLE, ROW_ID + "=" + rowNo, null);
}
}
但是当我在表中插入记录时出现以下错误:
10-21 20:33:11.044: I/Database(6597): sqlite 返回: 错误代码 = 1, msg = near ",": 语法错误
我已经仔细检查了所有语法,但没有得到任何解决方案。