0

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 void 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?

4
  • You need to put up the code where you create and instantiate the MyDatabaseManager object. More often than not, this happens because of context changes. Commented Sep 13, 2011 at 12:06
  • There is not much interesting stuff there, I get the context with using getApplicationContext() Commented Sep 13, 2011 at 12:33
  • What is the super call you're using?You have commented it out... Commented Sep 13, 2011 at 12:42
  • ok, I put it there. MyApp.getAndroidContext() simply returns the private static android.content.Context m_Context;, it is initialized within MyApp.onCreate() with the code m_Context = getApplicationContext(); Commented Sep 13, 2011 at 12:54

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

I changed the query like this and still doesnt work: create table t1 (t1key INTEGER PRIMARY KEY,data TEXT,num REAL,timeEnter NUMERIC);
Try increasing the version number of your database. It might be that the database was created the first time you ran your code but the table wasn't created. Now when you run your code, the onCreate() method won't get called because there is an empty database there.
As I mentioned I can confirm that onCreate() is called(I have a break point). I delete the database in the very beginning when my app starts running(for testing purposes).
All the code looks ok now. Show us the query or queries your using on the table.
You were right about the DATE but it was not the only problem. I fixed it now, thanks. +1
1

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.

1 Comment

I fixed it, _id column is not a requirement in Sqllite, the problem was something else, but thanks for helping. +1
1

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

1 Comment

I fixed it, that was not the problem but thanks for helping. +1
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.