1.创建MyDB.class类并继承SQLiteOpenHelper,然后实现onCreate,onUpgrade的方法并创建构造器。
2.下面是我写的一个类,写的不够好,有空再优化下。
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import java.util.HashMap;
import android.content.ContentValues;
import java.util.List;
import android.database.Cursor;
import java.util.ArrayList;
public class MyDb extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase p1) {
//创建user表
p1.execSQL("CREATE TABLE user"+
//添加id自增字段
"(id INTEGER primary key,"+
//添加整数型字段
"type INTEGER not null,"+
//添加文本类型字段
"content TEXT not null,"+
//添加自动添加时间字段
"date timestamp not null default (datetime('now','localtime')));");
}
@Override
public void onUpgrade(SQLiteDatabase p1, int p2, int p3) {
//删除表
p1.execSQL("DROP TABLE IF EXISTS user");
}
// 1. 创建 or 打开 可读/写的数据库(通过 返回的SQLiteDatabase对象 进行操作)
private SQLiteDatabase sql = getWritableDatabase();
public MyDb(Context context,String path)
{
// context 数据库文件地址
super(context, path, (SQLiteDatabase.CursorFactory) null, 1);
}
//删除
public void delete(int i)
{
this.sql.execSQL("DELETE FROM user WHERE id=" + i);
}
//添加
public boolean add(HashMap<String,Object> insertBean)
{
ContentValues contentValues = new ContentValues();
contentValues.put("type", new Integer( insertBean.get("type")));
contentValues.put("content",(String) insertBean.get("content"));
if (this.sql.insert("user", (String) null, contentValues) == ((long) -1))
{
return false;
}
return true;
}
//更新
public void update(int i, String txt)
{
ContentValues contentValues = new ContentValues();
contentValues.put("content", txt);
this.sql.update("user", contentValues, "id=?", new String[]{new StringBuffer().append(i).append("").toString()});
}
//查询所有
public List<HashMap<String,Object>> getAll()
{
ArrayList<HashMap<String,Object>> arrayList = new ArrayList<HashMap<String,Object>>();
Cursor query = this.sql.query("user", (String[]) null, (String) null, (String[]) null, (String) null, (String) null, (String) null);
while (query.moveToNext())
{
HashMap<String,Object> itemBean = new HashMap<String,Object>();
itemBean.put("id",query.getInt(query.getColumnIndex("id")));
itemBean.put("type",query.getInt(query.getColumnIndex("type")));
itemBean.put("content", query.getString(query.getColumnIndex("content")));
itemBean.put("date",query.getString(query.getColumnIndex("date")));
arrayList.add(itemBean);
}
return arrayList;
}
//查询最后一个
public HashMap getlastOne()
{
HashMap<String,Object> itemBean = new HashMap<String,Object>();
Cursor query = this.sql.rawQuery("SELECT * FROM user WHERE id = (SELECT MAX(id) FROM user)", null);
if (query.moveToNext())
{
itemBean.put("type",query.getInt(query.getColumnIndex("type")));
itemBean.put("id",query.getInt(query.getColumnIndex("id")));
itemBean.put("content", query.getString(query.getColumnIndex("content")));
}
return itemBean;
}
//分页
public ArrayList<HashMap<String,Object>> limit(int page){
ArrayList<HashMap<String,Object>> arrylist=new ArrayList<HashMap< String,Object>>();
String sql="select * from user order by id limit 10 offset "+page;
Cursor query = this.sql.rawQuery(sql, null);
while (query.moveToNext())
{
HashMap<String,Object> itemBean = new HashMap<String,Object>();
itemBean.put("type",query.getInt(query.getColumnIndex("type")));
itemBean.put("content", query.getString(query.getColumnIndex("content")));
arrylist.add(itemBean);
}
return arrylist;
}
}