[英]How can I create a new table in my SQLite Database using a param from a Java String variable?
I am trying to create a new table in SQLite using a preexisting Java String variable but the variable name is not being recognized as Java... How can I fix this?我正在尝试使用预先存在的 Java 字符串变量在 SQLite 中创建一个新表,但变量名未被识别为 Java ......我该如何解决这个问题?
String name1 = textViewPerson1.getText().toString();
String name2 = textViewPerson2.getText().toString();
String name3 = textViewPerson3.getText().toString();
try{
SQLiteDatabase database = this.openOrCreateDatabase("NewUsers",MODE_PRIVATE,null);
database.execSQL("CREATE TABLE IF NOT EXISTS newUsers (name VARCHAR, hitCount INT(2))");
database.execSQL("INSERT INTO newusers (name,age) VALUES (name1,17)");
database.execSQL("INSERT INTO newusers (name,age) VALUES (name2, 16)");
}catch (Exception e){
e.printStackTrace();
}
}
I recommend to you use quotes in the sql sentences and specify the value for your varchar, like :我建议您在 sql 语句中使用引号并为您的 varchar 指定值,例如:
database.execSQL("CREATE TABLE IF NOT EXISTS 'newUsers' ('name' VARCHAR(25), 'hitCount' INT(2))");
And you should exit the "String" Context in the insert lines, something like this :您应该在插入行中退出“字符串”上下文,如下所示:
database.execSQL("INSERT INTO 'newusers' (name,age) VALUES ('"+name1+"',17)");
Obviously there exist more cleans options like StringBuilder, but this should help.显然存在更多像 StringBuilder 这样的清理选项,但这应该会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.