帮一个大学生简单做了个记事本的功能,里面用到了数据库,正好,写一个使用SQLite的简单笔记。
首先创建一个类,继承SQLiteOpenHelper,然后重写里面的方法
package com.ztt.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDataBaseHelper extends SQLiteOpenHelper{
private static String databaseName="notepad.db";
private static int databaseVersion=1;
public MyDataBaseHelper(Context context) {
/**
* context 上下文
* DB_NAME 数据库名称
* null 默认游标工厂,从数据库头开始
* 1 数据库版本号,最小为1
*/
super(context, databaseName, null, databaseVersion);
}
/**
* 当数据库第一次被创建的时候调用,并且只调用一次,用来初始化数据库的表结构
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table login (id integer primary key autoincrement,username vachar(20),password vachar(30))");
//db.execSQL("create table income(id integer primary key autoincerement,username varchar(20),type varchar(20),date varchar(20),price varchar(20))");
db.execSQL("create table myincome(id integer primary key autoincrement,username varchar(20),type varchar(20),date varchar(20),price varchar(20))");
db.execSQL("create table pay(id integer primary key autoincrement,username varchar(20),type varchar(20),date varchar(20),price varchar(20))");
}
/**
* 当数据库被更新的时候调用,并且版本号只能升级,不能能降级
* 当版本号改变时,被调用,例如我们要更改表login的表结构机构,增加一个字段为性别
* oldVersion :旧的版本号,主要是用来判断是否是跨版本升级,从而用来执行到底增加几个字段,一半使用swich来写升级代码
* 不过现在我们一半会判断用户如果版本太老,直接让用户卸载重新下载安装
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//当旧的版本号为1增加性别和年龄字段,当旧的版本号为2增加性别字段,
/*switch (oldVersion) {
case 1:
db.execSQL("alter table login add sex varchar(20),age integer");
break;
case 2:
db.execSQL("alter table login add sex varchar(20)");
break;
}*/
}
}
然后写一些简单的增删改查方法代码
package com.ztt.database;
import com.ztt.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class LoginSqlTools {
/**
* 简单的关于登陆注册功能的数据库
*/
private MyDataBaseHelper dbHelper;
public LoginSqlTools(Context context){
//得到创建数据库的对象
dbHelper=new MyDataBaseHelper(context);
}
/**
* 添加登陆表
* @param username
* @param password
* @return -1代表添加失败
*/
public boolean addTable(String username,String password){
//得到一个可写的数据库
SQLiteDatabase db=dbHelper.getWritableDatabase();
//sqLiteDatabase.execSQL("insert into ? ()", bindArgs)
//传字段
ContentValues contentValues=new ContentValues();
contentValues.put("username", username);
contentValues.put("password", password);
long result=db.insert("login", null, contentValues);
if(-1==result){
//关闭流
db.close();
return false;
}else{
db.close();
return true;
}
}
/**
* 查询字段
* @param username
* @param password
* @return
*/
public boolean selectTable(String username,String password){
//得到一个可读数据库
SQLiteDatabase db=dbHelper.getReadableDatabase();
//查询login表,查询username和password两列,
Cursor cursor = db.query("login", new String[]{"username","password"}, "username=?", new String[]{username}, null, null, null);
boolean result=cursor.moveToNext();
if(result){
String pwd=cursor.getString(1);
if(pwd.equals(password)){
cursor.close();
db.close();
return true;
}else{
cursor.close();
db.close();
return false;
}
}else{
cursor.close();
db.close();
return false;
}
}
}
package com.ztt.database;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ztt.bean.*;
public class IncomeSqlTools {
private static MyDataBaseHelper dbhpler;
public IncomeSqlTools(Context context){
dbhpler=new MyDataBaseHelper(context);
}
public boolean addTable(String table,String name,String type,String date,String price){
SQLiteDatabase db=dbhpler.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("username", name);
contentValues.put("type", type);
contentValues.put("date", date);
contentValues.put("price", price);
long result= db.insert(table, null, contentValues);
if(-1!=result){
db.close();
return true;
}else{
db.close();
return false;
}
}
//查询所有的字段段
public List<IncomeBean> selectTable(String table){
List<IncomeBean> list=new ArrayList<IncomeBean>();
SQLiteDatabase db=dbhpler.getReadableDatabase();
//获取游标,判断是否有下一列数据
Cursor cursor=db.query(table, new String[]{"username","type","date","price"}, null, null, null, null, null);
while(cursor.moveToNext()){
String name=cursor.getString(0);
String type=cursor.getString(1);
String date=cursor.getString(2);
String price=cursor.getString(3);
IncomeBean incBean=new IncomeBean();
incBean.setName(name);
incBean.setType(type);
incBean.setDate(date);
incBean.setPrice(price);
list.add(incBean);
}
cursor.close();
db.close();
return list;
}
}
这是另外一张表,里面加了修改及删除
/**
* 删除
* 根据唯一id删除
* @param id
* @return
*/
public boolean deleteTable(String id){
SQLiteDatabase db=dbHelper.getWritableDatabase();
/**
* SQL_String.TABLE_MSG :表名
* SQL_String.mId :某个字段id名字
* SQL_String.mId+"=?" :条件,例如 where id=?
*/
int result=db.delete(SQL_String.TABLE_MSG, SQL_String.mId+"=?", new String[]{id});
if(-1!=result){
db.close();
return true;
}else{
db.close();
return false;
}
}
/**
* 修改
* @param id 唯一id
* @param week 某个字段,星期
* @param object 某个字段,项目
* @param start 某个字段,开始时间
* @param stop 某个字段,结束时间
* @return
*/
public boolean updateTable(String id,String week,String object,String start,String stop){
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
//将修改后的值放到集合中
values.put(SQL_String.mWeek, week);
values.put(SQL_String.mObject, object);
values.put(SQL_String.mStartTime, start);
values.put(SQL_String.mStopTime, stop);
/**
* values :修改后的值
* SQL_String.mId :某个字段id名字
* SQL_String.mId+"=?" :条件
*/
int result=db.update(SQL_String.TABLE_MSG, values, SQL_String.mId+"=?", new String[]{id});
/**
* 返回-1代表修改表失败
*/
if(-1!=result){
db.close();
return true;
}else{
db.close();
return false;
}
}