开发者

Android: sqlite - no such table exception

I have the following code (I simplified it & removed unrelevant parts)

public class MyDatabaseManager extends SQLiteOpenHelper {

    private SQLiteDatabase myDatabase;

    public DatabaseManager() {
    super(MyApp.getAndroidContext(), DATABASE_NAME, null, DATABASE_VERSION);

    myDatabase = getWritableDatabase();
    }

    @Override
    public v开发者_开发知识库oid onCreate(SQLiteDatabase database) {

    database.execSQL("create table t1 (t1key INTEGER PRIMARY KEY,data TEXT,num REAL,timeEnter NUMERIC);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    }

}

Now when I run queries against this database I get sqlite - no such table exception.

My breakpoint at database.execSQL hits and it doesn't raise any exception(for example if I change the code to database.execSQL("asda") I get syntax error exception) so I think my SQL code is correct. Yet the table is not created. I copied the database file to my pc and I looked in it with Sqlite browser and indeed my tables don't exist there. There is only one table and that is something called android_metadata. Any ideas?


Sqlite doesn't have a datatype for DATE. I would suggest changing it to an INTEGER and storing date.getTime() in it.


Change your query and try something like:

create table t1 (_id INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT,num REAL,timeEnter NUMERIC);

there should be a column _id in Android Sqlite Database table and better is it should be autoincrement.


Try passing the context when you instantiate the manager by changing the constructor as follows:

public MyDatabaseManager(Context ctx) {
 super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}

public openDB() throws SQLException
{
 myDatabase = getWritableDatabase; 
}

Now pass getApplicationContext() to the new MyDatabaseManager instance in the activity's onCreate():

MyDatabaseManager manager = new MyDataBaseManager(getApplicationContext());
manager.openDB();


Ok, I fixed the problem. There were multiple problems:
1) My create table query had problems
2) I was programatically copying the database file to the sd card at the end of the onCreate and apparently there it was not yet written. I moved it right under myDatabase = getWritableDatabase(); and it worked.

Thanks all for triying to help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜