package com.ninetripods.mq.sqliteupdatedemo.db;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ninetripods.mq.sqliteupdatedemo.MyApplication;
import com.ninetripods.mq.sqliteupdatedemo.bean.Student;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mq on 2017/10/22 上午11:36
* mqcoder90@gmail.com
*/
public class DBDao {
public static final String TABLE_NAME = "student";//表名
private static final String ID = "id";//id自增长
private static final String NAME = "stu_name";//姓名
private static final String AGE = "stu_age";//年龄
private static final String SEX = "stu_sex";//性别
private static final String GRADE = "stu_grade";//年级
//DB_Version2增加新字段
private static final String SCORE = "store";
private DBHelper dbHelper;
//创建表结构
public static final String SQL_CREATE_TABLE = "create table " + TABLE_NAME + "(" +
ID + " integer primary key autoincrement," +
NAME + " text," +
AGE + " integer," +
SEX + " varchar(5)," +
GRADE + " text" +
")";
private DBDao() {
dbHelper = new DBHelper(MyApplication.getContext());
}
public static DBDao getInstance() {
return InnerDB.instance;
}
private static class InnerDB {
private static DBDao instance = new DBDao();
}
/**
* 数据库插入数据
*
* @param bean 实体类
* @param <T> T
*/
public synchronized <T> void insert(T bean) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
try {
if (bean != null && bean instanceof Student) {
Student student = (Student) bean;
ContentValues cv = new ContentValues();
cv.put(NAME, student.getName());
cv.put(AGE, student.getAge());
cv.put(SEX, student.getSex());
cv.put(GRADE, student.getGrade());
cv.put(SCORE, student.getStore());
db.insert(TABLE_NAME, null, cv);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
db.close();
}
}
/**
* 删除表中所有的数据
*/
public synchronized void clearAll() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "delete from " + TABLE_NAME;
try {
db.execSQL(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
db.close();
}
}
/**
* 查询数据
*
* @return List
*/
public synchronized <T> List<T> query() {
SQLiteDatabase db = dbHelper.getReadableDatabase();
List<T> list = new ArrayList<>();
String querySql = "select * from " + TABLE_NAME;
Cursor cursor = null;
try {
cursor = db.rawQuery(querySql, null);
while (cursor.moveToNext()) {
Student student = new Student();
student.setName(cursor.getString(cursor.getColumnIndex(NAME)));
student.setAge(cursor.getInt(cursor.getColumnIndex(AGE)));
student.setSex(cursor.getString(cursor.getColumnIndex(SEX)));
student.setGrade(cursor.getString(cursor.getColumnIndex(GRADE)));
student.setStore(cursor.getString(cursor.getColumnIndex(SCORE)));
list.add((T) student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return list;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android数据库Sqlite的基本用法及升级策略
共80个文件
xml:38个
png:10个
java:8个
3星 · 超过75%的资源 需积分: 38 32 下载量 104 浏览量
2017-10-29
16:15:47
上传
评论
收藏 447KB ZIP 举报
温馨提示
http://www.jianshu.com/p/3fef519f3d8e Android数据库Sqlite的基本用法及升级策略
资源推荐
资源详情
资源评论
收起资源包目录
SqliteUpdateDemo.zip (80个子文件)
SqliteUpdateDemo
gradlew 5KB
settings.gradle 15B
.DS_Store 6KB
SqliteUpdateDemo.iml 870B
.idea
libraries
junit_4_12.xml 467B
support_vector_drawable_26_0_0_alpha1.xml 625B
javax_inject_1.xml 501B
support_annotations_26_0_0_alpha1.xml 554B
javax_annotation_api_1_2.xml 350B
exposed_instrumentation_api_publish_0_5.xml 636B
support_media_compat_26_0_0_alpha1.xml 616B
constraint_layout_solver_1_0_2.xml 346B
hamcrest_library_1_3.xml 334B
espresso_idling_resource_2_2_2.xml 618B
appcompat_v7_26_0_0_alpha1.xml 592B
hamcrest_core_1_3.xml 516B
jsr305_2_0_1.xml 322B
animated_vector_drawable_26_0_0_alpha1.xml 628B
support_core_utils_26_0_0_alpha1.xml 610B
constraint_layout_1_0_2.xml 401B
support_core_ui_26_0_0_alpha1.xml 601B
support_compat_26_0_0_alpha1.xml 598B
support_v4_26_0_0_alpha1.xml 586B
hamcrest_integration_1_3.xml 346B
espresso_core_2_2_2.xml 585B
rules_0_5.xml 546B
support_fragment_26_0_0_alpha1.xml 604B
javawriter_2_1_1.xml 322B
runner_0_5.xml 549B
runConfigurations.xml 564B
misc.xml 3KB
compiler.xml 686B
workspace.xml 198KB
gradle.xml 626B
encodings.xml 200B
modules.xml 369B
copyright
profiles_settings.xml 74B
gradle
wrapper
gradle-wrapper.properties 230B
gradle-wrapper.jar 52KB
.gradle
3.3
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 40KB
localClassSetAnalysis.lock 17B
taskArtifacts
fileHashes.bin 142KB
taskArtifacts.bin 113KB
fileSnapshots.bin 1.81MB
taskArtifacts.lock 17B
local.properties 431B
gradlew.bat 2KB
gradle.properties 730B
.gitignore 118B
app
.DS_Store 6KB
src
androidTest
java
com
ninetripods
mq
sqliteupdatedemo
ExampleInstrumentedTest.java 774B
test
java
com
ninetripods
mq
sqliteupdatedemo
ExampleUnitTest.java 413B
main
AndroidManifest.xml 767B
res
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
drawable
layout
activity_main.xml 804B
values
colors.xml 208B
strings.xml 79B
styles.xml 383B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
java
com
ninetripods
mq
sqliteupdatedemo
MyApplication.java 465B
db
DBDao.java 4KB
DBHelper.java 1KB
MainActivity.java 738B
bean
Student.java 1KB
update
DataBaseHelper.java 1KB
libs
app.iml 11KB
.gitignore 7B
build.gradle 950B
proguard-rules.pro 931B
build.gradle 498B
共 80 条
- 1
资源评论
- qq_316584772019-06-19还可以,能用需要自己修改下
- luoyin12082020-06-28不错,谢谢!!!
_小马快跑_
- 粉丝: 1w+
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功