如何使 SQLiteOpenHelper select 成为数据库而不是创建新数据库
How to make SQLiteOpenHelper select a database instead of creating a new database
从 SQLiteOpenHelper 继承并从继承的 class 中创建一个对象后,继承的 class 会创建一个全新的数据库来使用。如何让继承的 class 打开我之前创建的数据库而不是创建一个新数据库?
调用 SQLiteOpenHelper 构造函数时,传入数据库文件名。如果您使用相同的文件名,它将打开相同的数据库。虽然让多个 类 打开同一个数据库并不是我的建议。
我相信有人在这里问过这个问题:How to ship an Android application with a database?
我推荐 "Alex Jolig" 使用 SQLiteAssetHelper
的答案,如他们链接的 excellent tutorial 中所述。
If you are using a emulator then push the database file into the database folder of the project by opening android device manager->file Explorer->data->data->your project name then push the database file and then change the database name to your database name.
public class mydb extends SQLiteOpenHelper
{
String DBPATH="/data/data/com.example.aman.java/databases/";
static String DBNAME="java1db"; //here write your database name
static int DBVERSION=3;
Context mycontext;
SQLiteDatabase mydb1;
mydb(Context mycontext1)
{
super(mycontext1,DBNAME,null,DBVERSION);
mycontext=mycontext1;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public void init()
{
mydb1=this.getReadableDatabase();
}
public void close1()
{
mydb1.close();
}
}
if you are running on your device paste the database file in assets folder and paste this code in sqllitehelper class
void copyDatabase() throws IOException
{
InputStream myinput=mycontext.getAssets().open(DBNAME);
String outfile=DBPATH + DBNAME;
OutputStream myoutput=new FileOutputStream(outfile);
int a;
byte[] buffer=new byte[1024];
while((a=myinput.read(buffer))>0)
{
myoutput.write(buffer,0,a);
}
myoutput.close();
myinput.close();
}
从 SQLiteOpenHelper 继承并从继承的 class 中创建一个对象后,继承的 class 会创建一个全新的数据库来使用。如何让继承的 class 打开我之前创建的数据库而不是创建一个新数据库?
调用 SQLiteOpenHelper 构造函数时,传入数据库文件名。如果您使用相同的文件名,它将打开相同的数据库。虽然让多个 类 打开同一个数据库并不是我的建议。
我相信有人在这里问过这个问题:How to ship an Android application with a database?
我推荐 "Alex Jolig" 使用 SQLiteAssetHelper
的答案,如他们链接的 excellent tutorial 中所述。
If you are using a emulator then push the database file into the database folder of the project by opening android device manager->file Explorer->data->data->your project name then push the database file and then change the database name to your database name.
public class mydb extends SQLiteOpenHelper
{
String DBPATH="/data/data/com.example.aman.java/databases/";
static String DBNAME="java1db"; //here write your database name
static int DBVERSION=3;
Context mycontext;
SQLiteDatabase mydb1;
mydb(Context mycontext1)
{
super(mycontext1,DBNAME,null,DBVERSION);
mycontext=mycontext1;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public void init()
{
mydb1=this.getReadableDatabase();
}
public void close1()
{
mydb1.close();
}
}
if you are running on your device paste the database file in assets folder and paste this code in sqllitehelper class
void copyDatabase() throws IOException
{
InputStream myinput=mycontext.getAssets().open(DBNAME);
String outfile=DBPATH + DBNAME;
OutputStream myoutput=new FileOutputStream(outfile);
int a;
byte[] buffer=new byte[1024];
while((a=myinput.read(buffer))>0)
{
myoutput.write(buffer,0,a);
}
myoutput.close();
myinput.close();
}