如何在我的Android应用程序中使用.sqlite扩展名从资产文件夹sqlite数据库文件读取数据?Android:使用.sqlite扩展访问资产文件夹sqlite数据库文件
回答
如果您打算创建一个新的SQLite数据库,然后搭配并实施教程中所示的onCreate()方法。
但是,如果您使用的是由其他外部源创建的SQLite数据库,并且您要将其拉下,请将onCreate()方法留空。
重要的是,在本教程中,当您调用该文件时,请确保您传递应用程序上下文getApplicationContext()
,以便您有权访问正确的资产,否则可能会得到FileNotFound异常。
试试这个代码:
public class DataBaseHelper extends SQLiteOpenHelper {
private Context mycontext;
//private String DB_PATH = mycontext.getApplicationContext().getPackageName()+"/databases/";
private static String DB_NAME = "(datbasename).sqlite";//the extension may be .sqlite or .db
public SQLiteDatabase myDataBase;
/*private String DB_PATH = "/data/data/"
+ mycontext.getApplicationContext().getPackageName()
+ "/databases/";*/
public DataBaseHelper(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
boolean dbexist = checkdatabase();
if (dbexist) {
//System.out.println("Database exists");
opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if(dbexist) {
//System.out.println(" Database exists.");
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch(IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
} catch(SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/(packagename)/databases /(datbasename).sqlite");
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0) {
myoutput.write(buffer,0,length);
}
//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
public void opendatabase() throws SQLException {
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
if(myDataBase != null) {
myDataBase.close();
}
super.close();
}
}
不错,对我来说工作正常; d – 2013-04-19 08:06:32
实际上,在编码时使用mycontext.getApplicationInfo()。dataDir而不是硬编码“/ data/data /(packagename)”。这是获取应用程序数据目录的官方方式。 – wheredidthatnamecomefrom 2014-10-21 14:29:50
将旧数据库(old.db)在你的资源文件夹。键入这里面的onCreate()你的活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
//=======Code For copying Existing Database file to system folder for use====//
// Copying Existing Database into system folder
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/data.db";
File f = new File(destPath);
if(!f.exists()){
Log.v(TAG,"File Not Exist");
InputStream in = getAssets().open("old.db");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("TAG","ioexeption");
e.printStackTrace();
}
DBManager dbManager = new DBManager(this);
Log.v(TAG,"Database is there with version: "+dbManager.getReadableDatabase().getVersion());
String sql = "select * from prizes";
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
Log.v(TAG,"Query Result:"+cursor);
cursor.close();
db.close();
dbManager.close();
....
}
现在你必须做出一个DBManager类的子类SQLiteOpenHelper。插入抽象方法和构造函数。不要忘记在dbHelper的super()中输入正确的数据库名称。
public class DBManager extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DATABASES";
public DBManager(Context context) {
super(context, "data.db", null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG,"On create Called:"+db.getPath());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
现在,您可以通过实例化DBManager来访问数据库。
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
...
不要忘记关闭数据库或你会得到一个SQLiteDatabaseNotClosed异常。
db.close();
dbManager.close();
你会想尝试android sqlite asset helper。它为我打开了一个预先存在的db一块蛋糕。
我花了3个小时试图手动完成所有工作,大概花了半个小时。有趣的是,我认为我正在做图书馆为我做的同样的事情,但有些东西丢失了!
只能从资源文件夹读取数据库,因为资源文件夹是只读的。如果你需要做更多的操作,如创建,更新,删除,你可以做一个技巧。将资产数据库中的数据库复制到存储中,然后您可以执行任何您想要的操作。
这里是Working with Android Pre Built Database.
一个简单的例子有一个易于使用的库也从资产的文件夹中访问数据库。你可以检查Android SQLiteAssetHelper(https://github.com/jgilfelt/android-sqlite-asset-helper)。祝你好运!
您需要将.sqlite数据库转换为.db才能适应Android。
在你的应用首次启动安装
SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.COPY_TO_SYSTEM);
后,在随后的发布会
SuperDatabase database=new SuperDatabase(getApplicationContext(),"foods.db", AssetDatabaseMode.READ_FROM_DEVICE);
只要火SQL查询
database.sqlInject("INSERT INTO food VALUES('Banana','Vitamin A');");
取得成果在数组中CSV,JSON,XML
ArrayList<String> rows=new ArrayList<String>();
rows=database.sqlEjectCSV("SELECT * FROM food;");
for (int i=0;i<rows.size();i++)
{
//Do stuffs with each row
}
您需要为此包含我的库。此处的文档:
https://github.com/sangeethnandakumar/TestTube
- 1. 访问资产文件夹中的.sqlite数据库
- 2. android如何访问资产文件夹中包含的sqlite数据库
- 3. 如何访问android项目的资产文件夹中的sqlite数据库?
- 4. 通过访问SQLite在资产文件夹中显示数据
- 5. 从资产文件夹升级Android SQLite数据库
- 6. 在资产文件夹中打开sqlite数据库时出错
- 7. 从资产文件夹复制SQLite数据库
- 8. WinRT c#从文件夹资产加载sqlite数据库
- 9. 如何访问Android资产文件夹中的数据库?
- 10. SQLite:.db文件扩展名.sqlite文件扩展名为
- 11. 资产文件夹中的sqlite db
- 12. 资产访问文件夹
- 13. 如何在资源文件夹android中保存sqlite数据库?
- 14. SQLite数据库无法从资源文件夹的Android
- 15. 在Android中的资产文件夹中复制两个SQLite数据库
- 16. 从Android资产文件夹读取预先填充的SQLite数据库
- 17. 从资产文件夹创建的SQLite数据库更新问题
- 18. Android - SQLite数据库访问
- 19. 如何使用Sqlite for Android创建数据库文件(.sqlite)?
- 20. Android访问资产文件
- 21. 在android平台上扩展访问sqlite数据库
- 22. 插入文本文件数据到SQLite数据库使用android
- 23. 如何将预先存在的sqlite数据库文件从资产复制到数据库文件夹?
- 24. 附加资产文件夹中的数据库文件,以我自己的数据库SQLite的安卓
- 25. 如何在资产文件夹中保存.db或.sqlite文件中的DataBase.db或.sqlite文件在android
- 26. SQLite数据库文件
- 27. SQlite数据库文件的最佳扩展名是什么?
- 28. 删除android sqlite数据库文件
- 29. 如何将我的sqlite数据库转储回我的资产文件夹
- 30. 升级SQLite数据库,我是从资产的文件夹(安卓)
为什么将.sqlite文件放在资产文件夹中而不是/ res/raw? – Macarse 2010-04-09 11:09:45
你会想尝试[android sqlite资产助手](https://github.com/jgilfelt/android-sqlite-asset-helper)。它使它成为一块蛋糕。 – craned 2016-01-06 18:10:28