0

I am trying to create an SQLite Db for my android application, I have tried a lot of online tutorials and none of them work for me! I have posted my code below:

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "usersManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "users";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }



import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DatabaseHandler db = new DatabaseHandler(this);

I cant see error in my code but when I check in the location were db should be created nothing is there.

1
  • Thank u everyone for ur help, it worked for me! Commented Feb 3, 2013 at 18:18

6 Answers 6

4

Apart from avoiding long one lined concatenated strings:

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = 
            "CREATE TABLE " + TABLE_CONTACTS + 
            "(" + 
                KEY_ID   + " INTEGER PRIMARY KEY," + 
                KEY_NAME + " TEXT" + //--last value has no comma--
            ")";

    db.execSQL(CREATE_CONTACTS_TABLE);
}

Call getWritableDataBase() to open database, it will be created if it is not already there.

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

Comments

3

Try this

public class DataBase extends SQLiteOpenHelper{


    static String createBDSQL = "CREATE TABLE Notas (id integer primary key autoincrement, title TEXT)";

    public DataBase(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS Notas");
            db.execSQL(createBDSQL);
    }
}

In MainActivity.java

DataBase notasdb = new DataBase(this, "DBSample.db", null, 1);
SQLiteDatabase db = notasdb.getWritableDatabase();

1 Comment

i think it's better to keep final Strings in the database class
1

The SQLiteOpenHelper is lazy. It will not perform any action until necessary. Thus, your DB will only be created when you try to access data in it (or insert new data).

Comments

0

You have to be carefull when creating a query for to execute.. in particular with spaces and commas.

change this line

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);

whit:

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + " (" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT)";
        db.execSQL(CREATE_CONTACTS_TABLE);

Comments

0

You really didn't created database, you only create instance of your SQLiteOpenHelper but for create or open database you have to use getWritableDatabase() or getReadableDatabase() method.

SQLiteDatabase db = new DataBaseHandler(this).getWritableDatabase();

Comments

0

in your main activity

SQLiteDatabase sql;
sql=db.getWritableDatabase(); (where db is DB Handler)

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.