1

can anyone tell me why this doesn't work?

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);
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Did u try db = openOrCreateDatabase("database.db", Context.MODE_PRIVATE, null) – san Jan 09 '12 at 09:22
  • if you run in the emulator, you can `cd` to your database folder, and can use sql sentence to check the database and table. – idiottiger Jan 09 '12 at 09:39

5 Answers5

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

I Think you can write it like this:

 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)"; 
deepak Sharma
  • 1,641
  • 10
  • 23
  • if you want to check the table exist or not then you can check by query http://stackoverflow.com/questions/1601151/how-do-i-check-in-sqlite-whether-a-table-exists – deepak Sharma Jan 09 '12 at 10:52
  • i think it worked, how can i see my database, the easiest way please – bunnyannihilator89 Jan 09 '12 at 10:54
  • oh if you want to see your database you need to get its .sqlite file from file-Explorer/data/data/your_Package_name/database/your_file_name.db pull the file from their to desktop,Then change its extension with .sqlite and open it with sqliteManager in firefox it shows you all data. – deepak Sharma Jan 09 '12 at 11:02
0

Replace these two lines:

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)"; 
abhishek ameta
  • 194
  • 1
  • 12
0

Have you tried adding semicolons to your sql statements?

Instead of:

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

Try:

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

I know in Oracle it's important. I know in my SQLite I always include them and mine works. I think the ";" in Oracle/SQLite kind of works like the "go" in Sybase.

Harvey
  • 19
  • 5
0

db.execSQL("INSERT INTO playlist (name) Values('" + name + "')");

nantha
  • 53
  • 6
0

//change your String telValue to int.

int telValue = Integer.parseInt(tel.getText().toString());
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130