1

I'm trying to create a simple database in android, but the database doesn't created and i don't know why. I read many solutions but nothing help.

This is the SQLiteOpenHelper extended class

public class TodoDB extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "todo.db";

    public TodoDB(android.content.Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String query = "CREATE TABLE "+ TaskSchema.TaskColumns.TABLE_NAME + "(" +
                TaskSchema.TaskColumns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                TaskSchema.TaskColumns.TITLE + " TEXT NOT NULL, " +
                TaskSchema.TaskColumns.INFORMATION + " TEXT NOT NULL, " +
                TaskSchema.TaskColumns.DATE + " TEXT NOT NULL " +
                ");";
        sqLiteDatabase.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void addTask()
    {
        ContentValues values = new ContentValues();
        values.put(TaskSchema.TaskColumns.TITLE, "l");
        values.put(TaskSchema.TaskColumns.INFORMATION, "temporal");
        values.put(TaskSchema.TaskColumns.DATE,"20-8-16");
        this.getWritableDatabase().insert(TaskSchema.TaskColumns.TABLE_NAME,null,values);
    }

    public String getTask()
    {
        String args[] = {"l"};
        String columns[] = {TaskSchema.TaskColumns.TITLE};
        Cursor c = this.getReadableDatabase().query(TaskSchema.TaskColumns.TABLE_NAME,columns, TaskSchema.TaskColumns.TITLE + " LIKE ?",args,null,null,null);
        String result = c.getString(c.getColumnIndex(TaskSchema.TaskColumns.TITLE));
        return result;
    }

}

This is the activity class

public class MainActivity extends AppCompatActivity {

    Button addButton;
    Button deleteButton;
    TodoDB db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addButton =  (Button)findViewById(R.id.addButton);
        TodoDB db = new TodoDB(this);
        db.addTask();
        String test = db.getTask();
        Toast.makeText(this, test,Toast.LENGTH_SHORT);
    }
}

Pleas help.

Extra information: I used the Android monitor to find the database, but isn't in the virtual device and I ran the app in my cellphone and when I am looking for the database, it doesn't exists. when I try to do a query the app close.

  • how do you know that the database is not created? – lelloman Aug 11 '17 at 15:59
  • Try to add semicolon (;) at the end of query. Query should now be like: String query = "CREATE TABLE "+ TaskSchema.TaskColumns.TABLE_NAME + "(" + TaskSchema.TaskColumns.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TaskSchema.TaskColumns.TITLE + " TEXT NOT NULL, " + TaskSchema.TaskColumns.INFORMATION + " TEXT NOT NULL, " + TaskSchema.TaskColumns.DATE + " TEXT NOT NULL " + ");"; – W.Kurek Aug 11 '17 at 16:03
  • @lelloman I used the Android monitor to find the database, but isn't in the virtual device and I run the app in my cellphone and when I am looking for the database, it doesn't exists. when I try to do a query the app close. – Ulises Madero Aug 11 '17 at 16:03
  • @W.Kurek it doesn't work – Ulises Madero Aug 11 '17 at 16:14
  • @UlisesMadero Which error/exception causes app to close while you're doing a query? – W.Kurek Aug 11 '17 at 16:17
  • @W.Kurek The getTask() method above. – Ulises Madero Aug 11 '17 at 16:42

2 Answers2

1
TodoDB db = new TodoDB(this);

Instantiating a SQLiteOpenHelper does not create the database file. You need to call getWritableDatabase() or getReadableDatabase() on it to create the database file, too. For example:

TodoDB openHelper = new TodoDB(this);
SQLiteDatabase db = openHelper.getWritableDatabase();
laalto
  • 150,114
  • 66
  • 286
  • 303
-1

I don't know what you have done before. Have you created any other tables before trying to create this table? If yes, the onCreate() method in SQLite is called only the first time you call the class which implements it. Try deleting your app, and installing it again. Or give some other details of what you have done before

John P
  • 1,159
  • 2
  • 18
  • 44