public class sqlitedemo extends SQLiteOpenHelper {
public sqlitedemo(Context context) {
// String name, CursorFactory factory,
super(context, "xiaoer", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table two(_id integer primary key autoincrement,name varchar(30),age varchar(10))");
Log.i("sqlite","oncreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.i("sqlite","onUpgrade");
}
}
需要继承自SQLiteOpenHelper
super(context, "xiaoer", null, 1);
有4个参数第二个是数据库的名字 第三个是用来操作表的填null默认用系统自带的 1是数据库的版本
varchar: 是类型参数可以指定对应数的大小
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
是再版本升级的时候会调用oncreate之会调用一次 可以进行数据库表的操作等
例如:增加一个字段
db.execSQL("alter table two add heights integer");
增删该查demo
public class sqliteStudent {
private SQLiteDatabase db;
public sqliteStudent(Context context, sqliteTableOpenHelper table) {
super();
db = table.getWritableDatabase();
Log.e("haha", db.toString());
}
public void add(Student s) {
db.execSQL("insert into student values(null,?,?)",
new Object[] { s.getName(), s.getNumber() });
}
public void delete(Student s) {
db.execSQL("delete from student where age=?",
new Object[] { s.getNumber() });
}
public void update(Student s) {
db.execSQL("update student set name=?", new Object[] { "haha" });
}
public Student query(Student s) {
Cursor rawQuery = db.rawQuery("select *from student where age=?",
new String[] { s.getNumber() + "" });
while (rawQuery.moveToNext()) {
}
return new Student("xaioer", 20);
}
}
demo::
package com.example.sqlitedemo3;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class studentSqlite {
private sqliteManager sq;
private SQLiteDatabase db;
public studentSqlite(Context context) {
super();
sq = new sqliteManager(context);
db = sq.getWritableDatabase();
}
public void add(Student s) {
db.execSQL("insert into student values(null,?,?)",
new Object[] { s.getName(), s.getAge() });
}
public void delete(Student s) {
db.execSQL("delete from student where name=?",
new Object[] { s.getName() });
}
public void update(Student s) {
db.execSQL("update student set name=?", new Object[] { s.getName() });
}
public List<Student> query() {
db = sq.getReadableDatabase();
Cursor rawQuery = db.rawQuery("select *from student", null);
List<Student> datas = new ArrayList<Student>();
Student s = new Student();
while (rawQuery.moveToNext()) {
//得到每行对应字段的索引通过索引得到对应的内容
s.setName(rawQuery.getString(rawQuery.getColumnIndex("name")));
s.setAge(rawQuery.getString(rawQuery.getColumnIndex("age")));
datas.add(s);
}
//记得关闭啊
rawQuery.close();
return datas;
}
}
package com.example.sqlitedemo3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class sqliteManager extends SQLiteOpenHelper {
public sqliteManager(Context context) {
super(context, "students", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table student(_id integer primary key autoincrement,name varchar(30),age varchar(10))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}