gpt4 book ai didi

android - 数据库插入时出错

转载 作者:行者123 更新时间:2023-11-30 04:18:16 25 4
gpt4 key购买 nike

我已经编写了数据库类并尝试将值插入数据库,但我得到了这样的异常 android.database.sqlite.SQLiteException: near "15": syntax error: , while compiling:

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

问题是什么

我的数据库类是

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);
}
});

最佳答案

尝试:

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);
}
});

你失去了一些“','”

关于android - 数据库插入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com