java - Android Java,数据库插入,无法正常工作

原文 标签 java android android-emulator

谁能告诉我为什么这行不通?

db = openOrCreateDatabase("database.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
                 db.setLocale(Locale.getDefault());
                 db.setLockingEnabled(true);
                 db.setVersion(1); 

                 String dropTableA = "DROP TABLE if exists databaseA";
                 String createTableA = "CREATE TABLE databaseA(" +
                 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                 "Nome TEXT, Telefone INTEGER)";

                 String dropTableB = "DROP TABLE if exists databaseB";
                 String createTableB = "CREATE TABLE databaseB(" +
                 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                 "Localidade TEXT, " +
                 "fk_dbA_id INTEGER NOT NULL CONSTRAINT dbA_id REFERENCES databaseA(_id) ON " +
                 "DELETE CASCADE)";

                 String nomeValue = nome.getText().toString();
                 String telValue = tel.getText().toString();
                 String localValue = local.getText().toString();
                 //PROBLEM MAY LIE HERE
                 String insertA = "insert into databaseA(_id, Nome, Telefone) values(1, "+nomeValue+", "+telValue+")"; 
                 //PROBLEM MAY LIE HERE
                 String insertB = "insert into databaseB(_id, Localidade, fk_dbA_id) values(1, "+localValue+", 1)"; 

                 db.execSQL(dropTableA);
                 db.execSQL(createTableA);
                 db.execSQL(dropTableB);
                 db.execSQL(createTableB);
                 db.execSQL(insertA);
                 db.execSQL(insertB);

最佳答案

 String insertA = "insert into databaseA(_id, Nome, Telefone) values(1, "+nomeValue+", "+telValue+")"; 
             //PROBLEM MAY LIE HERE
             String insertB = "insert into databaseB(_id, Localidade, fk_dbA_id) values(1, "+localValue+", 1)"; 


我想你可以这样写:

 String insertA = "insert into databaseA(_id, Nome, Telefone) values(1, \""+nomeValue+"\", "+telValue+")"; 
             //PROBLEM MAY LIE HERE
             String insertB = "insert into databaseB(_id, Localidade, fk_dbA_id) values(1, \""+localValue+"\", 1)"; 

相关文章:

android - 当Recyclerview不在第一项上时,如何禁用swiperefreshlayout?

java - 如果发生超时异常,则重新订阅一个可观察对象

android - HTC设备ADB错误

android - 关闭后,Android模拟器无法启动

javascript - AngularJS身份验证问题

java - 使用XPath检索XML元素

java - Eclipse + GWT类路径错误

java - Android-使用签名的APK时应用崩溃

java - 从ADT仿真器报告堆栈跟踪

java - Websphere应用服务器 - 开始任何快速的操作到底是什么?