1
public class MyAct extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SQLiteDatabase db = openOrCreateDatabase("MyDb",MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS MyTab (Month INT(2),Date INT(2),Year INT(4),Event VARCHAR;");
        db.execSQL("INSERT INTO MyTab VALUES (0,1,2012,'mini_proj');");
        db.close();

    }
}

I have written this small snippet to create a table, but it's not working. What is the problem with this code?

3
  • what do you mean by not working? is it a crash? Commented Dec 15, 2012 at 15:29
  • 1
    You're missing a ). Also, remove the ; Commented Dec 15, 2012 at 15:30
  • @nandeesh no...in ddms it is not showing the name of database table Commented Dec 15, 2012 at 15:31

1 Answer 1

2

In your first query, you are missing a closing brace:

CREATE TABLE IF NOT EXISTS MyTab (
    Month INT(2),
    Date INT(2),
    Year INT(4),
    Event VARCHAR;

It should be:

CREATE TABLE IF NOT EXISTS MyTab (
    Month INT(2),
    Date INT(2),
    Year INT(4),
    Event VARCHAR
)

Also, note that the query doesn't need to end with a ;, as mentioned in the docs:

Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported.

Also, you should ALWAYS check your LogCat output, since this should throw a SQLiteException.


Some further SQLite notes:

  1. SQLite doesn't have a VARCHAR-type. It only has TEXT and will convert any text-like type into it.
  2. Note that giving a length for a datatype is also ignored by SQLite:

SQLite does not impose any length restrictions (other than the large global SQLITE_MAX_LENGTH limit) on the length of strings, BLOBs or numeric values.

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

4 Comments

I modified my code.but still its not reflecting in ddms.now what does that mean?
Read your LogCat output. Also, Datbase things should go into a SQliteDatabaseHelper.
Thanks for your replies.Ya...i have included SQLiteDatabaseHelper.
Actually i am using a date picker.i want to compare date selected in date picker with the date stored in database table,and display the event on that day.Whether my approach is right?please suggest me..

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.