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,不然会造成内存泄漏
-
@param path
*/
public static void setWorkPath(Context context, String path) {
mContext = context;
if (null != getRootPath()) {
mWorkPath = mRootPath + path;
}
if (!mWorkPath.endsWith(“/”)) {
mWorkPath += “/”;
}
File file = new File(mWorkPath);
if (!file.exists()){
boolean b = file.mkdirs();
}
}
public static String getDBpath() {
File file = new File(mWorkPath + DB_PATH_NAME);
if (!file.exists())
file.mkdirs();
return mWorkPath + DB_PATH_NAME;
}
public static void copyAccessDB(Context context) {
try {
String[] dbNames = context.getAssets().list(“db”);
for (String dbName : dbNames) {
long startTime = System.currentTimeMillis();
String filePath = FileUtil.getDBpath() + dbName;
File dbFile = new File(filePath);
if (!dbFile.exists()) {
FileOutputStream fos = null;
try {
dbFile.createNewFile();
}catch (Exception e){
}
InputStream is = context.getAssets().open(“db/” + dbName);
fos = new FileOutputStream(dbFile);
byte[] buffer = new byte[2048];
int len = -1;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
long endTime = System.currentTimeMillis();
long useTime = endTime - startTime;
copyTime += useTime;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Android 本地 操作数据库逻辑(查,删,改)
package com.example.testdemo.util;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import com.example.testdemo.bean.User;
import java.util.ArrayList;
import java.util.List;
public class DBManager {
private Context mContext;
private SQLiteDatabase mDB;
private static String dbPath = FileUtil.getDBpath() + “/Test.db”;
private static DBManager instance = null;
public DBManager() {
}
public static DBManager getInstance() {
if (instance == null) {
instance = new DBManager();
}
return instance;
}
/**
- 打开数据库
*/
private void openDB() {
if (isSDCard()) {
if (mDB == null || !mDB.isOpen())
mDB = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
}
private boolean isSDCard() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
//查询选择题
public List queryUser() {
List userList = new ArrayList();
User user= null;
openDB();
try {
String sql = " select a.id,a.name,a.age,a.phoneNum,b.name as sexName from user a,gender b where a.sex= b.type";
Cursor cursor = mDB.rawQuery(sql, null);
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(“id”));
String name = cursor.getString(cursor.getColumnIndex(“name”));
String sex = cursor.getString(cursor.getColumnIndex(“sexName”));
String age = cursor.getString(cursor.getColumnIndex(“age”));
String phoneNum = cursor.getString(cursor.getColumnIndex(“phoneNum”));
user= new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setPhoneNum(phoneNum);
user.setSex(sex);
【附】相关架构及资料
源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
user.setPhoneNum(phoneNum);
user.setSex(sex);
【附】相关架构及资料
源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。
[外链图片转存中…(img-5b4jQccU-1714454435720)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!