02-19 16:46:01.677: E/AndroidRuntime(13159): android.database.sqlite.SQLiteException: no such table: notes

StackOverflow https://stackoverflow.com/questions/9350686

Question

I'm having this error when I try to access one table:

02-19 16:46:01.677: E/AndroidRuntime(13159): android.database.sqlite.SQLiteException: no such table: notes: INSERT INTO notes (user,text) VALUES ('john','testingvalue')

But I created the table! I have two tables in the database ('users' and 'notes'), I'm accessing them the same way, but when I try to insert a value in the table 'notes', I have that error.

That's the code:

package com.loopr;

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQL extends SQLiteOpenHelper {

    String createUserTable = "CREATE TABLE users (name TEXT)";
    String createNotesTable = "CREATE TABLE notes (user TEXT, text TEXT)";

    public SQL(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createNotesTable);
        db.execSQL(createUserTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int before, int after) {
        //Nothing at the moment
    }

}

What can I do? Thanks.

Was it helpful?

Solution

Try changing int version and put this in your onUpgrade overridable method:

// Drop the old table.
db.execSQL("DROP TABLE IF EXISTS notes");

// Create a new one.
onCreate(db);

OTHER TIPS

What can you do? Learn to debug on Android. Read the material at this link. Figure out how to use DDMS's File Explorer to pull databases from an emulator. Get a tool like SQLite Manager (firefox) to look at these databases - it's helped me enormously. Also, you can use SQLite Manager to perform raw sql statements on your DB, allowing you to craft sql and test at the same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top