Take the 2-minute tour ×

I needed to store some data related to class periods in an Android project. After looking at my options, I thought a SQL database would be a good choice. Unfortunately, I can't seem to get my statement to actually open a database. My open database statement string goes like this:

"create table "+ DATABASE_PERIOD+" (_id integer primary key autoincrement,"
+KEY_CLASSTITLE+" text not null, "+KEY_PERIOD+" text not null, 
"+KEY_XPERIODS+" text not null, "+KEY_DOUBLEPERIODS+" text not null);

I based it off of the Notepad project on the Android website. I just added a few more fields, but it is unable to open the database. If you guys want the error message or some other kind of info, I'll try to get it for you (This is my first time with SQL so I don't really know what is needed to fix this up). Thanks in advance!

The error message I'm getting goes like this:

android.database.sqlite.SQLiteException: no such table: periodData: , while
compiling: SELECT DISTINCT _id, title, period, xPeriods, doublePeriods FROM
periodData WHERE _id = -1

And as it says there, I'm using SQLite.

My code to put some data into the table in my Database Helper class:

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CLASSTITLE, title);
initialValues.put(KEY_PERIOD, period);
initialValues.put(KEY_XPERIODS, xPeriods);
initialValues.put(KEY_DOUBLEPERIODS, doublePeriods);

return mDb.insert(DATABASE_PERIOD, null, initialValues);

I started using private static strings to ensure that my calls aren't incorrect (Thanks Barak for the catch!)

share|improve this question
This statement does not open a database (-connection), it creates a table in a database. Add some more code and the full Stack-Trace to your question. –  Lukas Knuth Apr 10 '12 at 22:59
Do provide the error messages you're getting. Also provide what database implementation you're using (SQLite, MySQL, PostGres, SQL Server, etc.) –  Nathaniel Ford Apr 10 '12 at 23:10
add comment

2 Answers

up vote 1 down vote accepted

Your issue is the table name (as the error message indicates). From the code you show you created a table called "period". Yet you try to query it using the table name "periodData".

Two ways to fix it:

1) Change the table name in your database to periodData (more difficult as it involves re-creating the db).

2) Change the table name in your query from "periodData" to "period".

share|improve this answer
Nice catch! But it still is getting the same error message. I edited the question to show how I put in the data, which I'm guessing might have a few errors in it. Thanks! –  Matt Heartney Apr 12 '12 at 21:14
I'll make a wild guess that you changed the table name and forgot to increment the DB version variable, so the DB didn't get deleted/recreated with your new table name. If that is the case, simply increment that variable, run the app and it should be fixed. –  Barak Apr 12 '12 at 21:25
That was it! Everything is now working great. Thank you for directing me through this whole thing. –  Matt Heartney Apr 13 '12 at 21:33
add comment

Check out the dev topic on http://developer.android.com/guide/topics/data/data-storage.html#db

You'll create a DbHelper class to help you open the database and your code will look something like:

SQLiteDatabase db = dbHelper.getReadableDatabase();

Hope this was helpful.

share|improve this answer
Based on the additional info in the exception you've added, @Barak 's post seems to hit the nail on the head. –  John N Apr 11 '12 at 5:23
add comment

Your Answer


