-1

I got a very simple example (inside a button onClickListener, just for information):

DatabaseHandler dbHandler = new DatabaseHandler(
                v.getContext(), 
                v.getContext().getResources().getString(R.string.DATABASE_NAME));

dbHandler.getWritableDatabase().execSQL("CREATE TABLE IF NOT EXISTS test (abc TEXT);");

dbHandler.getWritableDatabase().rawQuery("INSERT INTO test (abc) VALUES ('blah');", null); 

Cursor test = dbHandler.getReadableDatabase().rawQuery("SELECT * FROM test;", null);
Log.e("TEST", test.toString());
Log.e("TEST", String.valueOf(test.getCount()));

Class:

public class DatabaseHandler extends SQLiteOpenHelper {

private static int DATABASE_VERSION = 2;

public DatabaseHandler(Context context, String dbName) {
    super(context, dbName, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
}

}

Output (its a button, I clicked it 3 times):

04-07 01:31:51.047: E/TEST(11816): android.database.sqlite.SQLiteCursor@42e97400
04-07 01:31:51.047: E/TEST(11816): 0
04-07 01:31:51.677: E/TEST(11816): android.database.sqlite.SQLiteCursor@42ed3980
04-07 01:31:51.677: E/TEST(11816): 0
04-07 01:31:52.428: E/TEST(11816): android.database.sqlite.SQLiteCursor@42e755d8
04-07 01:31:52.438: E/TEST(11816): 0

I just don't find whats wrong with it. A couple of minutes ago I got it working. I don't know why it stopped working all of sudden though. Does anybody see my mistake?

Philip
  • 1,068
  • 3
  • 12
  • 21
  • 1
    `test` is the Cursor that shall hold the results. Take a look again, you probably just overlooked it. – Philip Apr 06 '14 at 23:35
  • what do you mean? `test` is instantiated in the line before I use it with `Log.e` – Philip Apr 06 '14 at 23:36
  • My bad. Can you show which code invokes the button, or otherwise calls your code? This is not quite an SCCE. – Pedantic Apr 06 '14 at 23:41
  • 1
    trust me, all of that calling stuff works. otherwise I wouldn't have that logcat output, right. and also, when i delete the `IF NOT EXISTS` it gives me an error, telling me the table already exists, so that works. simply the INSERT/SELECT doesn't. But okay, here it is: `btn_scan = (ImageButton)rootView.findViewById(R.id.btn_scan);` `btn_scan.setOnClickListener(btn_scan_listener);` ... `static OnClickListener btn_scan_listener = new OnClickListener() { @Override public void onClick(View v) {`... – Philip Apr 06 '14 at 23:42
  • I don't trust you, and the code you are showing (namely your database handler class) doesn't have anything to do with your error. Boil it down to something some random person from the internet can copy & paste into a test method and give it a go. – Pedantic Apr 06 '14 at 23:44
  • well, youre good to go. copy the databasehandler class and copy the code that calls it.. it should work. only you have to create a string in your resources called `DATABASE_NAME`.. – Philip Apr 06 '14 at 23:45
  • 2
    I don't think your INSERT query is executing. – Arunabh Das Apr 06 '14 at 23:45
  • Your database handler class does absolutely nothing. Maybe that's your problem.? – Pedantic Apr 06 '14 at 23:45
  • I tried to catch an error with try/catch, but nothing was catched.. – Philip Apr 06 '14 at 23:45
  • Maybe fill in those `//TODO`'s? – Pedantic Apr 06 '14 at 23:46
  • I think the INSERT query is executed, since when I change the table to `testWHATEVER` it tells me it is not existing, obviously. so it must be executed.. – Philip Apr 06 '14 at 23:53
  • @Das any suggestions? :/ – Philip Apr 07 '14 at 00:01

1 Answers1

1

Seems like rawQuery does not work for INSERT-Statements. Using execSQL works.

Philip
  • 1,068
  • 3
  • 12
  • 21
  • Yes because if you don't move the returned cursor, the SQL is not executed. See http://stackoverflow.com/questions/20110274/what-is-the-correct-way-to-do-inserts-updates-deletes-in-android-sqlitedatabase/20118910#20118910 – laalto Apr 07 '14 at 06:17