android 读取本地数据库db文件(Android sqlite)

1)拷贝数据库涉及到读写 ,所以权限这块需要注意,如果是22以上的需要申请权限,否则会报错。

2)assets文件夹是在main文件夹下面建和res是平级,之前很多来面试的还把文件夹都放错了。

3)读取用户时候,性别一栏是需要做关联查询的 ,因为用户表性别用的是字典值。

Android拷贝逻辑代码

package com.example.testdemo.util;

import android.content.Context;

import android.os.Environment;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

public class FileUtil {

private static String mWorkPath = null;

private static String mRootPath = null;

private static Boolean mGetSDpath = false;

private final static String DB_PATH_NAME = “database/”;

public static long copyTime = 0;

private static Context mContext;

public static String getRootPath() {

if (!mGetSDpath || mRootPath == null) {

mGetSDpath = true;

boolean sdCardExist = Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED); // 判断sd卡是否存在

if (sdCardExist) {

File sdDir = Environment.getExternalStorageDirectory();// 获取跟目录

mRootPath = sdDir.toString();

} else {

mRootPath = mContext.getFilesDir().toString();

}

}

if (!mRootPath.endsWith(“/”))

mRootPath += “/”;

return mRootPath;

}

/**

  • 设置工作目录

  • @param context app context,不然会造成内存泄漏

  • 29
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,用于从SQLite数据库读取数据并在ListView中显示: 1. 创建一个数据库帮助类 DBHelper.java: ```java public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private static final String COLUMN_ID = "id"; private static final String COLUMN_NAME = "name"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String query = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT);"; db.execSQL(query); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } public void insertData(String name) { SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, name); db.insert(TABLE_NAME, null, values); db.close(); } public ArrayList<String> getData() { ArrayList<String> dataList = new ArrayList<>(); SQLiteDatabase db = getReadableDatabase(); String query = "SELECT * FROM " + TABLE_NAME; Cursor cursor = db.rawQuery(query, null); if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME)); dataList.add(name); } while (cursor.moveToNext()); } cursor.close(); db.close(); return dataList; } } ``` 2. 在Activity中使用DBHelper类来读取数据并在ListView中显示: ```java public class MainActivity extends AppCompatActivity { private ListView listView; private ArrayList<String> dataList; private ArrayAdapter<String> adapter; private DBHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.list_view); dataList = new ArrayList<>(); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList); listView.setAdapter(adapter); dbHelper = new DBHelper(this); dbHelper.insertData("John"); dbHelper.insertData("Mary"); dbHelper.insertData("Tom"); ArrayList<String> list = dbHelper.getData(); dataList.clear(); dataList.addAll(list); adapter.notifyDataSetChanged(); } } ``` 注意:需要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值