Sqlite数据库的创建与方法

前端之家收集整理的这篇文章主要介绍了Sqlite数据库的创建与方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//首先先创建一个数据库

public class MysqLiteOpenHelper extends sqliteOpenHelper {




public MysqLiteOpenHelper(Context context) {
super(context,"school",null,1);
}


@Override
public void onCreate(sqliteDatabase db) {

db.execsql("create table student1(id varchar(5),student varchar(10))");
System.out.println("onCreate");



}


@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
System.out.println("onUpgrade");


}

//下面提供dao方法

public class Dao {


private MysqLiteOpenHelper helper;
Context context;

public Dao(Context context) {
super();
this.context = context;
helper = new MysqLiteOpenHelper(context);
}

//添加方法
public void add(String id,String student)
{
sqliteDatabase db = helper.getWritableDatabase();

ContentValues values= new ContentValues();
values.put("id",id);
values.put("student",student);
db.insert("student1",values);
db.close();
}

//删除方法
public void delete(String id)
{
sqliteDatabase db = helper.getWritableDatabase();
db.delete("student1","id=?",new String[]{id});
db.close();
}

//查询方法
public void select()
{
sqliteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query("student1",null);
while(cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex("id"));
String student = cursor.getString(cursor.getColumnIndex("student"));
System.out.println("id:"+id+"student:"+student);
}
db.close();
}

//修改方法

public void update(String id,String student) { sqliteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("student",student); db.update("student1",values,new String[]{id}); db.close(); }

原文链接:https://www.f2er.com/sqlite/198244.html

猜你在找的Sqlite相关文章