Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite database not created

I am using standard approach which I learned from popular notepad example for building my first app.
But I got stuck into a strange problem.
The SQLite database file (.db) is not being created in /data/data/package/databases. I have cross checked manifest file and OpenHelper file from HeadFirst android dev. Book ( I am using it as a reference ) but I found myself unable to figure out why the database file is not created..
Are there any special permissions or something we need to switch on to get it working??
strange thing is that its working fine for pre-loaded examples. means '.db' file is created for notepad example but when I try, it fails. :(
here are my files.

Package - database.test

1. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Database_testActivity"
        android:label="@string/app_name" >
    </activity>
</application>

2.TestOpenHelper.java

package database.test;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TestOpenHelper extends SQLiteOpenHelper {

TestOpenHelper(Context context){
    super(context,"mydb.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase database){
    database.execSQL("create table test_1 " + " (field1 integer primary key, field2 integer);");
}
public void onUpgrade (SQLiteDatabase database, int oldVersion, int newVersion){
    database.execSQL("drop table if exists test1");
    onCreate(database);
}
}

3.Database_testActivity.java

package database.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

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

    ListView listview = (ListView)findViewById(R.id.test_list);
    TestAdapter ta = new TestAdapter();
    listview.setAdapter(ta);

    TestOpenHelper openHelper = new TestOpenHelper(this);
}
}   

I know its a very basic question, but I have been trying to solve this for last fifteen days and now I am completely frustrated.
Please help ..

like image 819
Bond Avatar asked Jun 04 '12 20:06

Bond


People also ask

Where is SQLite database created?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases. However, there are no restrictions on creating databases elsewhere.

Can we create database in SQLite?

To create a new database in SQLite you need to specify databases when opening SQLite, or open an existing file using . open . You can use the . database command to see a list of existing databases.

What is the correct way to create database in SQLite?

In SQLite, the sqlite3 command is used to create a new database. Syntax: sqlite3 DatabaseName. db.


1 Answers

Change this:

database.execSQL("create table test_1 " + " (field1 integer primary key, field2 integer)");

and this TestOpenHelper openHelper = new TestOpenHelper(this); change to SQLiteDatabase db = new TestOpenHelper(this).getWritableDatabase();

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.

Exactly from docs:

Create and/or open a database that will be used for reading and writing.

like image 109
Simon Dorociak Avatar answered Sep 23 '22 05:09

Simon Dorociak