Android 未创建 sqlite 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10888227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlite database not created
提问by Bond
I am using standard approach which I learned from popular notepad example for building my firstapp.
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.
我正在使用从流行的记事本示例中学到的标准方法来构建我的第一个应用程序。
但我陷入了一个奇怪的问题。
未在 /data/data/package/databases 中创建 SQLite 数据库文件 (.db)。我已经交叉检查了 HeadFirst android dev 的清单文件和 OpenHelper 文件。书(我用它作为参考)但我发现自己无法弄清楚为什么没有创建数据库文件..
是否有任何特殊权限或我们需要打开的东西才能让它工作?
奇怪的是,它对于预加载的示例工作正常。意味着 '.db' 文件是为记事本示例创建的,但是当我尝试时,它失败了。:(
这是我的文件。
Package - database.test
包 - database.test
1. AndroidManifest.xml
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
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
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 ..
我知道这是一个非常基本的问题,但过去 15 天我一直在努力解决这个问题,现在我完全感到沮丧。
请帮忙 ..
回答by Simon Dorociak
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();
并且这个TestOpenHelper openHelper = new TestOpenHelper(this);
更改为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.
您真的没有创建数据库,您只创建了您的实例,SQLiteOpenHelper
但是对于创建或打开数据库,您必须使用getWritableDatabase()
或getReadableDatabase()
方法。
Exactly from docs:
完全来自文档:
Create and/or open a database that will be used for reading and writing.
创建和/或打开将用于读取和写入的数据库。