Sqlite简单操作
1.student类
public class Student { int id;
String name;
String sex;
String address;
int money; public Student(int id, String name, String sex, String address, int money) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.address = address;
this.money = money;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", address=" + address + ", money=" + money + "]";
} }
2.studentDao
import java.util.ArrayList;
import java.util.List; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; public class StudentDao { /**
* 插入数据
*/ public boolean insert(Student stu) {
SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase();
try {
db.execSQL("insert into student (name,sex,address,money) values(?,?,?,?)",
new Object[]{stu.getName(),stu.getSex(),stu.getAddress(),stu.getMoney()});
db.close();
return true;
} catch (Exception e) {
// TODO: handle exception
} return false;
}
/**
* 删除数据
* @param student
*/
public void delete(int id) {
SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase();
//db.execSQL("delete from student where _id = ?", new Object[]{student.getId()});
db.execSQL("delete from student where _id = "+id);
db.close();
} /**
* 更新数据
*/ public void update(Student stu) {
SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase();
db.execSQL("update student set name = ?,sex=?,address=?,money=? where _id = ?",
new Object[]{stu.getName(),stu.getSex(),stu.getAddress(),stu.getMoney(),stu.getId()});
db.close();
} /**
* 根据Id查询数据
* @param id
* @return
*/
public Student findById(int id) {
SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase();
Cursor cursor = db.rawQuery("select * from student where _id = ?", new String[]{id+""});
Student student = null;
if (cursor.moveToNext()) {
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
String address = cursor.getString(cursor.getColumnIndex("address"));
int money = cursor.getInt(cursor.getColumnIndex("money"));
student = new Student(_id, name, sex, address, money);
}
return student;
} /**
* 查询全部数据
* @return
*/
public List<Student> findAll() {
List<Student> list = new ArrayList<Student>();
SQLiteDatabase db = SQLiteDBHelper.getSqLiteDatabase(); Cursor cursor = db.rawQuery("select * from student", null);
Student student = null;
while (cursor.moveToNext()) {
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
String address = cursor.getString(cursor.getColumnIndex("address"));
int money = cursor.getInt(cursor.getColumnIndex("money"));
student = new Student(_id, name, sex, address, money);
list.add(student);
}
return list;
} }
3.SqliteDbHelper
import java.io.File; import android.database.sqlite.SQLiteDatabase; import com.qf.day14_sqlitedatabase02.SDcardHelper; public class SQLiteDBHelper {
private static String DB_PATH = SDcardHelper.getSDCardPath();
private static String DB_NAME = "student.db"; static {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH+File.separator+DB_NAME, null);
db.execSQL("create table if not exists student(_id integer primary key autoincrement," +
"name varchar(20)," +
"sex varchar(4)," +
"address varchar(100)," +
"money integer)");
db.close();
} public static SQLiteDatabase getSqLiteDatabase() {
return SQLiteDatabase.openOrCreateDatabase(DB_PATH+File.separator+DB_NAME, null);
} }
4.SDcardHelper
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import android.os.Environment;
import android.os.StatFs; public class SDcardHelper {
/**
* 判断Sdcard是否可用
*/ public static boolean isSDcardMounted() {
boolean state = Environment.getExternalStorageState().
equals(Environment.MEDIA_MOUNTED);
return state;
} /**
* 获取SDcard目录
* @return
*/
public static String getSDCardPath() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
return path;
} /**
* 返回总大小
* @return
*/
public static long getSDCardSize() {
if (isSDcardMounted()) {
//
StatFs sf = new StatFs(getSDCardPath());
long count = sf.getBlockCount();
long size = sf.getBlockSize();
return count * size;
}
return 0;
} /**
* 返回剩余空间
*/ public static long getSDCardFreeSize() {
if (isSDcardMounted()) {
StatFs sFs = new StatFs(getSDCardPath());
long count = sFs.getFreeBlocks();
long size = sFs.getBlockSize();
return count * size;
}
return 0;
} /**
* 获取可用空间
*/ public static long getSDCardAvailableSize() {
if (isSDcardMounted()) {
StatFs sFs = new StatFs(getSDCardPath());
long count = sFs.getAvailableBlocks();
long size = sFs.getBlockSize();
return count * size;
}
return 0;
} /**
*
* @param data 保存的数据
* @param dir 保存的文件或者目录
* @param filename 保存的文件名
* @return
*/
public static boolean saveFileToSDCard(byte[] data, String dir,String filename) {
if (isSDcardMounted()) {//判断Sdcard是否可用
File filedir = new File(getSDCardPath() + File.separator + dir);
if (!filedir.exists()) {//是否存在目录,不存在则创建
filedir.mkdirs();
} if (getSDCardAvailableSize() >= data.length) {//判断空间是否够用
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(filedir+File.separator+filename));
fos.write(data);
fos.flush();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} return false;
} public static byte[] readFileFromSDCard(String filepath) {
if (isSDcardMounted()) {
File file = new File(filepath);
ByteArrayOutputStream byteArrayOutputStream = null;
if (file.exists()) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = fileInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, length);
} return byteArrayOutputStream.toByteArray();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
}
return null;
}
}
5.mainActivity
import java.util.List; import com.qf.day14_sqlitedatabase02.bean.Student;
import com.qf.day14_sqlitedatabase02.dao.StudentDao; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity {
StudentDao studentDao = new StudentDao();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 数据库的Id从1开始,不是下标
* @param v
*/
public void insert(View v) {
studentDao.insert(new Student(2, "老冯", "男", "航海路阳光城", 30000000));
studentDao.insert(new Student(2, "老魏", "男", "东建材", 30000));
studentDao.insert(new Student(2, "老欧", "男", "西海岸", 900000));
studentDao.insert(new Student(2, "萍姐", "女", "农业东路中州大道", 100000));
studentDao.insert(new Student(2, "和珅", "男", "清朝紫荆城", 9000000)); }
public void delete(View v) {
studentDao.delete(1);
}
public void update(View v) {
studentDao.update(new Student(3, "标哥", "男", "南京", 10));
}
public void findId(View v) {
Student student = studentDao.findById(4);
Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
}
public void findAll(View v) {
List<Student> list = studentDao.findAll();
Toast.makeText(this, list.toString(), Toast.LENGTH_SHORT).show();
}
}
Sqlite简单操作的更多相关文章
- 我的Android六章:Android中SQLite数据库操作
今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...
- 安卓 SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- 在安卓开发中使用SQLite数据库操作实例
前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...
- iOS 中SQLite数据库操作
在iOS中实现SQLite数据库的操作:1.导入框架(libsqlite3.0.tbd) 2.导入头文件<sqlite3.h> 3.实现数据的增删改查 实现简单 SQLite数据库操作 的 ...
- iOS - SQLite Database 操作数据库
iOS - SQLite Database 操作数据库 Sqlite 能被用在ios上做数据处理用,只要你懂得一点sql 就很容易使用sqlite 1:创建一个简单的View based appl ...
- C# SQLite 数据库操作
C# SQLite 数据库操作学习 运行环境:Window7 64bit,.NetFramework4.61,C# 7.0 参考: SQLite 官网 SQL As Understood By SQL ...
- SQLite简单介绍
一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1)归档:NSCodeing.NSKeyed ...
- Python学习笔记:sqlite3(sqlite数据库操作)
对于数据库的操作,Python中可以通过下载一些对应的三方插件和对应的数据库来实现数据库的操作,但是这样不免使得Python程序变得更加复杂了.如果只是想要使用数据库,又不想下载一些不必要的插件和辅助 ...
- Android SQLite 简单使用演示样例
SQLite简单介绍 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也很的强大. 袖珍型的SQLite能够支持高达2TB大小的数据库, ...
随机推荐
- oracle 表锁定解锁
Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作,那么这些操作是怎么实现的呢?本文我们主要就介绍一下这部分内容.(1)锁表查询的代码有以下的形式:select count( ...
- lambda表达式、匿名函数
lambda表达式是函数式编程中的匿名函数语法规范. In computer programming, an anonymous function (function literal, lambda ...
- 洛谷P2038 无线网络发射器选址 水题 枚举
刚开始边界写错了(将128写成127). 注意n <= 20,所以可以每读入一个点就将其周边更新,这样最多也只会有 40 * 40 * 20 种位置需要被枚举. Code: #include&l ...
- Python 之 格式化文件
# 结构化文件存储- xml, json- 为了解决不同设备之间的信息交换 ## XML文件(可扩展标记语言) - 标记语言:语言中使用尖括号括起来的文本字符串标记 - 可扩展:用户可以自己定义需要的 ...
- java+selenium自动化遇到confirm弹窗,出现NoAlertPresentException: no alert open
//操作js的confirm弹窗,bool控制是否点击确定,true为点击确定,false为点击取消 public static void OperaterJSOfConfirm(WebDriver ...
- js js键盘各键对应的代码 ---转
0x1 鼠标左键\r 0x2 鼠标右键\r 0x3 CANCEL 键\r 0x4 鼠标中键\r 0x8 BACKSPACE 键\r 0x9 TAB 键\r 0xC CLEAR 键\r 0xD ENTE ...
- PatentTips - Uncore thermal management
BACKGROUND The field of invention relates to the computer sciences, generally, and, more specificall ...
- BA--暖通系统常见设计细节要点
(一)系统设计问题 1.水泵在系统的设计位置: 一般而言,冷冻水泵应设在冷水机组前端,从末端回来的冷冻水经过冷冻水泵打回冷水机组:冷却水泵设在冷却水进机组的水路上,从冷却塔出来的冷却水经冷却水泵打回机 ...
- Java的五大原则
五个基本原则: 单一职责原则(Single-Resposibility Principle):一个类,最好只做一件事,只有一个引起它的变化.单一职责原则可以看做是低耦合.高内聚在面向对象原则上的引申, ...
- spring容器启动过程理解
一.一切从手动启动IoC容器开始 ClassPathResource resource = new ClassPathResource("bean.xml"); DefaultLi ...