插入时数据库错误

时间:2012-03-12 10:31:34

标签: android database sqlite

我写了数据库类&试图在数据库中插入值,但我得到像这样的android.database.sqlite.SQLiteException异常:接近“15”:语法错误:,编译时:

insert into tbl_setmessage (Title,Message,DateFrom,StartTime,ExpiryDate,ExpiryTime)        values 
('Swapnil','hii','12-3-2012 '15:53'14-3-2012 '15:54')

问题是什么

我的Databse课程是

public class DataBaseCreator {
private static final String DATABASE_NAME = "SetMessage.db";
private static final int DATABASE_VERSION = 1;
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;

public DataBaseCreator(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
}


public long insert(String TABLE_NAME, String TABLE_FIELDS, String TABLE_VALUES) {
    String INSERT = "insert into " 
            + TABLE_NAME + "("+ TABLE_FIELDS +") values ("+TABLE_VALUES+")";
    this.insertStmt = this.db.compileStatement(INSERT);
    return this.insertStmt.executeInsert();
}
public void closeDb() {
    this.db.close();
}  

private static class OpenHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE Set_message (Tittle VARCHAR(30) NOT NULL," +
     "Message VARCHAR(30) NOT NULL,"+
        "Date_from DATETIME NOT NULL,"+
         "Start_Time DATETIME NOT NULL,"+
        "Expiry_Date DATETIME NOT NULL,"+
         "Expiry_Time DATETIME NOT NULL)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP DATABASE IF EXISTS " + DATABASE_NAME);
        onCreate(db);
    }

}

public void open() {
    // TODO Auto-generated method stub

}

}

和主要类是

btn_SetMessage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            baseCreator.insert("tbl_setmessage ","Title,Message,DateFrom,StartTime,ExpiryDate,ExpiryTime","'"+tv1.getText()+"','"
                    +tv2.getText()+"','"+StartDate.getText()+"'"+StartTime.getText()+"'"+ExpiryDate.getText()+"'"+ExpiryTime.getText()+"'");
            Intent i=new Intent(SetMessage.this,SelectLocation.class);
            startActivity(i);
        }
    });

3 个答案:

答案 0 :(得分:1)

尝试:

btn_SetMessage.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        baseCreator.insert("tbl_setmessage ","Title,Message,DateFrom,StartTime,ExpiryDate,ExpiryTime","'"+tv1.getText()+"','"
                +tv2.getText()+"','"+StartDate.getText()+"','"+StartTime.getText()+"','"+ExpiryDate.getText()+"','"+ExpiryTime.getText()+"'");
        Intent i=new Intent(SetMessage.this,SelectLocation.class);
        startActivity(i);
    }
});

你输了一些“','”

答案 1 :(得分:0)

这是您的错误消息,我冒昧地重新格式化:

  

我写了数据库类&试图在数据库中插入值,但我得到像这样的例外   android.database.sqlite.SQLiteException:接近“15”:语法错误:,同时编译:插入到tbl_setmessage(Title,Message,DateFrom,StartTime,ExpiryDate,ExpiryTime)值('Swapnil','hii',' 12-3-2012 '15:53'14-3-2012 '15:54')

正如您在粗体部分所看到的,有些逗号丢失了。试试这个:

insert into tbl_setmessage (Title,Message,DateFrom,StartTime,ExpiryDate,ExpiryTime) values ('Swapnil','hii','12-3-2012', '15:53', '14-3-2012', '15:54')

小心撇号(符号')。

另外,为什么不使用Android SQLite APIs?他们很方便。您也可以使用SQLiteQueryBuilder

答案 2 :(得分:0)

    //decleration
  EventDataSQLHelper eventsData;// class where database is created
    SQLiteDatabase dbx ;
    SQLiteDatabase rdbx;
     ContentValues values;
//onCreate
 eventsData = new EventDataSQLHelper(NewWakeupProfile.this);

        dbx = eventsData.getWritableDatabase();    
        rdbx = eventsData.getReadableDatabase();
        values=new ContentValues();
 values.put("columnname1",value1);  
 values.put("columnname1",value2);  
 values.put("columnname1",value3);  
 values.put("columnname1",value4);
.......
......
.......


//insert query
dbx.insert("tablename", null,values);