我正在尝试研究并创建一个简单的数据库,创建一个只有一行的表。我首先看到它在我使用SQLiteHelper的'update()'方法时有效。但是,当我使用SQL命令'UPDATE ... SET'尝试它时,它会抛出一个'没有这样的列'的错误
我不知道为什么我不能使用'UPDATE ... SET'命令,因为'update'方法没有问题。如果有人知道android SQL,你能帮帮我吗?以下是我的代码和错误消息。
CODE:
public class TestDBMain extends Activity {
private final String SETTINGS_TABLE_NAME = "settings";
// Columns
private final String DEV_ID = "dev_id";
private final String SERVER_NAME = "server_name";
private final String SERVER_PORT = "server_port";
private final String SERVER_SUBFOLDER = "server_subfolder";
private final String USERNAME = "username";
private final String PASSWORD = "password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DB_Settings db_s = new DB_Settings(this);
db_s.deleteDBSettings();
db_s.addDBSettings(SERVER_NAME, "test_server_01");
// ERROR occurs here
db_s.addDBSettingsWithSQLCommand(SERVER_NAME, "test_server_22");
}
public class DB_Settings extends SQLiteOpenHelper {
private static final String DB_NAME = "settings.db";
private static final int DATABASE_VERSION = 1;
private String[] Settings_FROM = { _ID, DEV_ID, SERVER_NAME, SERVER_PORT, SERVER_SUBFOLDER,
USERNAME, PASSWORD};
private Context mContext;
public DB_Settings(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "+SETTINGS_TABLE_NAME+"("+_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
+DEV_ID+" VARCHAR,"
+SERVER_NAME+" VARCHAR,"
+SERVER_PORT+" INTEGER,"
+SERVER_SUBFOLDER+" VARCHAR,"
+USERNAME+" VARCHAR,"
+PASSWORD+" VARCHAR);");
initialize(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void deleteDBSettings(){
mContext.deleteDatabase(DB_NAME);
}
private void initialize(SQLiteDatabase db){
ContentValues values = new ContentValues();
values.put(DEV_ID, "1");
values.put(SERVER_NAME, "1");
values.put(SERVER_PORT, 443);
values.put(SERVER_SUBFOLDER, "1");
values.put(USERNAME, "1");
values.put(PASSWORD, "1");
db.insertOrThrow(SETTINGS_TABLE_NAME, null, values);
}
public void addDBSettings(String key, Object value){
// Insert a new record
SQLiteDatabase db = getWritableDatabase();
ContentValues table_value = new ContentValues();
if(value.getClass().equals(String.class)){
table_value.put(key, value.toString());
}
else if (value.getClass().equals(Integer.class)){
table_value.put(key, (Integer)value);
}
// add to database
db.update(SETTINGS_TABLE_NAME, table_value, "_id=1", null);
}
public void addDBSettingsWithSQLCommand(String key, Object value){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("UPDATE "+SETTINGS_TABLE_NAME
+" set "+key+" = "+value.toString()
+" where _id = 1;");
}
}
错误日志:
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): Caused by: android.database.sqlite.SQLiteException: no such column: test_server_22: UPDATE settings set server_name = test_server_22 where _id = 1;
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): at android.test.db.TestDBMain$DB_Settings.addDBSettingsWithSQLCommand(TestDBMain.java:109)
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): at android.test.db.TestDBMain.onCreate(TestDBMain.java:35)
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-20 01:00:53.516: ERROR/AndroidRuntime(6066): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:4)
对于查询字符串,你必须输入'和'。试试这个
db.execSQL("UPDATE "+SETTINGS_TABLE_NAME
+" set "+key+" = '"+value.toString()
+"' where _id = 1;");
答案 1 :(得分:1)
尝试此查询以进行更新..
db.execSQL("UPDATE "+SETTINGS_TABLE_NAME
+" set "+key+" = \""+value.toString()
+"\" where _id = 1;");