如何将SQLite数据库连接到android应用程序?
救命啊!好吧,我设计安卓应用已经有相当一段时间了,我一直在手动将所有这些数据放入字符串中,然后在我的布局中将它们拉出来,但后来我的一个朋友建议我把所有必要的数据都放进数据库,然后在每次活动中都把它们从数据库中取出……听起来不错……我已经阅读了一本又一本教程,了解这是如何工作的,这似乎比制作大量的字符串和教程中的示例各自服务要困难得多不是我的目的,也不会让我更容易理解。所有我需要这个数据库做的是读取和显示的信息,我希望它的布局。我用SQLite数据库浏览器创建了这个数据库 数据库结构:如何将SQLite数据库连接到android应用程序?,android,database,sqlite,Android,Database,Sqlite,救命啊!好吧,我设计安卓应用已经有相当一段时间了,我一直在手动将所有这些数据放入字符串中,然后在我的布局中将它们拉出来,但后来我的一个朋友建议我把所有必要的数据都放进数据库,然后在每次活动中都把它们从数据库中取出……听起来不错……我已经阅读了一本又一本教程,了解这是如何工作的,这似乎比制作大量的字符串和教程中的示例各自服务要困难得多不是我的目的,也不会让我更容易理解。所有我需要这个数据库做的是读取和显示的信息,我希望它的布局。我用SQLite数据库浏览器创建了这个数据库 数据库结构: 名称-鱼索
名称-鱼索引_db
表-鱼、州、注册
行:
鱼号、名称、描述、位置
状态-_id,名称,缩写,已更新
注册号、姓名、尺码、季节、数量、备注
现在假设我想在布局列表视图中显示reg表中主键(_id)12的所有内容,这是如何实现的?需要.java和.xml代码示例。这是两个教程,您可以使用它们来帮助您实现目标:
- -这篇文章进一步向您展示了如何连接到生产数据库和使用web服务
希望这能有所帮助。看看连接到fishindex\u db的代码
public class SQLiteHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "fishindex_db.db";
public static final String TABLE_NAME = "fish";
public static final String _id = "id";
public static final String name = "name";
public static final String desc = "desc";
private SQLiteDatabase database;
SQLiteHelper sQLiteHelper = new SQLiteHelper(MainActivity.this);
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//this is to get all records in fish table
public ArrayList<FishModel> getAllRecords() {
database = this.getReadableDatabase();
Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);
ArrayList<FishModel> fishes= new ArrayList<FishModel>();
FishModel fishModel;
if (cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToNext();
fishModel= new FishModel();
fishModel.setID(cursor.getString(0));
fishModel.setName(cursor.getString(1));
fishModel.setLastName(cursor.getString(2));
fishes.add(fishModel);
}
}
cursor.close();
database.close();
return contacts;
}
public类SQLiteHelper扩展SQLiteOpenHelper{
私有静态最终int数据库_VERSION=1;
公共静态最终字符串数据库\u NAME=“fishindex\u db.db”;
公共静态最终字符串表\u NAME=“fish”;
公共静态最终字符串_id=“id”;
公共静态最终字符串name=“name”;
公共静态最终字符串desc=“desc”;
专用数据库;
SQLiteHelper SQLiteHelper=新的SQLiteHelper(MainActivity.this);
公共SQLiteHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//这是为了获取fish表中的所有记录
公共阵列列表getAllRecords(){
database=this.getReadableDatabase();
Cursor Cursor=database.query(表名称,null,null,null,null,null);
ArrayList=新的ArrayList();
FishModel-FishModel;
if(cursor.getCount()>0){
对于(int i=0;i