}
}
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();
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
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);
userList.add(user);
}
cursor.close();
return userList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void updateUser() {
openDB();
String sql = " update user set name = ‘李四’ where name = ‘王杰’ ";
mDB.execSQL(sql);
}
public void deleteUser(String id) {
openDB();
mDB.delete(“user”, " id = ? ", new String[]{id});
}
}
基本最核心的就这些代码,不是很复杂,贴上效果图。 我知道有的小伙伴需要完整的DEMO,所以我就整理了一个 ,在这里**DEMO下载**。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。 |
标签: #android读取数据库数据 #IF #return #context #APP