我有一个名为str_train_no
的字符串,我想创建一个名称相同的数据库,到目前为止我已经尝试了
String name=str_Train_no+".db";
SQLiteDatabase traindb = openOrCreateDatabase(name,SQLiteDatabase.CREATE_IF_NECESSARY , null);
sql="CREATE TABLE IF NOT EXISTS "+str_Train_no+" ("
+"Stops VARCHAR);";
traindb.execSQL(sql);
下面是数据库声明和插入部分的源代码
SQLiteDatabase db = openOrCreateDatabase( "Train_list.db", SQLiteDatabase.CREATE_IF_NECESSARY , null);
try {
final String CREATE_TABLE_TRAIN_LIST = "CREATE TABLE IF NOT EXISTS Train_list ("
+ "Train_name VARCHAR,"
+ "Train_no VARCHAR,"
+ "Train_start VARCHAR,"
+ "Train_end VARCHAR,"
+ "Seats_Available VARCHAR);";
db.execSQL(CREATE_TABLE_TRAIN_LIST);
Toast.makeText(admin_manipulation.this, "Table created", Toast.LENGTH_LONG).show();
String sql = "INSERT or replace INTO Train_list (Train_name, Train_no, Train_start, Train_end, Seats_Available) VALUES('"+str_Train_name + "',' " +str_Train_no + "', '" +str_Train_start+"','" +str_Train_end+"',' " +str_Train_seats +"');";
db.execSQL(sql);
Toast.makeText(admin_manipulation.this, "Inserted Sucessfully", Toast.LENGTH_SHORT).show();
String name=str_Train_no+".db";
SQLiteDatabase traindb = openOrCreateDatabase(name, SQLiteDatabase.CREATE_IF_NECESSARY , null);
sql="CREATE TABLE IF NOT EXISTS "+str_Train_no+" ("
+"Stops VARCHAR);";
traindb.execSQL(sql);
Toast.makeText(admin_manipulation.this, "Table "+str_Train_no+" verified", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(admin_manipulation.this, "An Error has occured", Toast.LENGTH_SHORT).show();
}
这会不断抛出异常。 有什么想法吗? 提前谢谢!
答案 0 :(得分:0)
使用str_train_no
数字字符串时,您遇到的问题是您尝试创建一个名称不是有效SQL标识符的表。
在这种特殊情况下,你可以通过在文本字符串前加上来解决它:
sql = "CREATE TABLE IF NOT EXISTS train"+str_Train_no+ ...
在您的情况下,您还可以使用静态表名,因为您为每列火车创建了一个数据库(这可能不是最好的事情)。
答案 1 :(得分:-1)
使用text而不是varchar。据我所知,SQLite中没有varchar。