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

时间:2012-01-09 09:17:58

标签: 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);

5 个答案:

答案 0 :(得分: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)"; 

我认为你可以这样写:

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

答案 1 :(得分:0)

替换这两行:

String insertA = "insert into databaseA(_id, Nome, Telefone) values(1, '"+nomeValue+"', '"+telValue+"')"; 

         String insertB = "insert into databaseB(_id, Localidade, fk_dbA_id) values(1, '"+localValue+"', 1)"; 

答案 2 :(得分:0)

您是否尝试过在您的sql语句中添加分号?

而不是:

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

尝试:

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

我在Oracle中知道这很重要。我知道在我的SQLite中我总是包含它们和我的作品。我觉得 ”;”在Oracle / SQLite中,类似于Sybase中的“go”。

答案 3 :(得分:0)

db.execSQL(“INSERT INTO播放列表(名称)值('”+ +,+“)”);

答案 4 :(得分:0)

//将您的String telValue更改为int。

int telValue = Integer.parseInt(tel.getText().toString());