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);
...
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
}
7条答案
按热度按时间4ioopgfo1#
试试这个代码:
字符串
qeeaahzv2#
将旧数据库(old.db)放入资产文件夹。在Activity的onCreate()中键入以下内容:
字符串
现在你需要创建一个DBManager类,它是SQLiteOpenHelper的子类。插入抽象方法和构造函数。不要忘记在dbHelper的super()中输入正确的数据库名称。
型
现在你可以通过示例化DBManager来访问数据库。
型
不要忘记关闭数据库,否则你会得到一个SQLiteDatabaseNotClosed异常。
型
jyztefdp3#
重要的是,在本教程中,当您调用文件时,请确保传递应用程序上下文
getApplicationContext()
,以便您可以访问正确的资产,否则您可能会获得FileNotFound异常。gwo2fgha4#
你会想尝试android sqlite asset helper,它让打开一个预先存在的数据库对我来说是小菜一碟。
我真的有它的工作在大约半个小时后,花了3个小时试图做这一切手动。有趣的是,我以为我在做同样的事情图书馆为我做的,但有些东西不见了!
relj7zay5#
您需要将.sqlite数据库转换为.db以适应Android。
安装后首次启动应用时
字符串
在以后启动时
型
只需执行SQL查询
型
在CSV、JSON、XML格式的数组中获取结果
型
你需要把我的图书馆包括进来。文档在这里:
https://github.com/sangeethnandakumar/TestTube
xkrw2x1b6#
如果您计划创建一个新的SQLite数据库,那么请重写并实现onCreate()方法,如教程所示。
但是,如果您使用的SQLite数据库是由另一个外部源创建的,并且您打算将其拉下,那么将onCreate()方法留空。
w9apscun7#
您只能从资产文件夹中读取数据库,因为资源文件夹是只读的。如果您需要执行更多操作,如创建,更新,删除,您可以做一个技巧。将数据库从资产文件夹复制到存储,然后您可以做任何您想要的。
下面是一个Working with Android Pre Built Database.的快速示例
有一个易于使用的库也从资产文件夹访问数据库。你可以检查Android SQLiteAssetHelper(https://github.com/jgilfelt/android-sqlite-asset-helper)。祝你好运!