Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

android - 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 ..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...