SQLite数据库学习笔记

SQLite数据库学习笔记,第1张

概述     这两天学习了 SQLite数据库,做下学习笔记。SQLite数据库为嵌入型数据库。 1、抽象类SQLiteOpenHelper为数据库建立连接,其子类DBOpenHelper实现父类onCreate、和onUpgrade并调用父类的构造方法 /** * */package edu.fjnu.sqlite.service;import android.content.Conte

这两天学习了 sqlite数据库,做下学习笔记。sqlite数据库为嵌入型数据库。

1、抽象类sqliteOpenHelper为数据库建立连接,其子类DBOpenHelper实现父类onCreate、和onUpgrade并调用父类的构造方法

/** *  */package edu.fjnu.sqlite.service;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;/** * @author Kysann * */public class DBOpenHelper extends sqliteOpenHelper {	    private static final String DB_name = "stumgr.db";    private static final Integer DB_VERSION = 1;    	public DBOpenHelper(Context context) {				super(context,DB_name,null,DB_VERSION);			}	@OverrIDe	public voID onCreate(sqliteDatabase db) {				db.execsql("create table student(_ID Integer primary key autoincrement,stu_name varchar(30),stu_pwd varchar(30))");        	}	@OverrIDe	public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {		// Todo auto-generated method stub	}}

2、通过 DBOpenHelper 的 dbHelper.getWritableDatabase(); / dbHelper.getReadableDatabase();获取sqliteDatabase实例进行对数据库的 *** 作。


/** *  */package edu.fjnu.sqlite.service;import java.util.ArrayList;import java.util.List;import edu.fjnu.sqlite.domain.Student;import androID.content.Context;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;/** * @author Kysann * */public class StudentService {		private static final String sql_INSERT = "insert into student values(null,?,?)";	private static final String sql_UPDATE = "update student set stu_name=?,stu_pwd=? where _ID=?";	private static final String sql_DELETE = "delete from student where _ID=?";	private static final String sql_LOAD   = "select * from student limit ?,?";	private static final String sql_SELECT = "select * from student where _ID=?";	private static final String sql_COUNT  = "select count(*) from student";		private DBOpenHelper dbHelper;	private sqliteDatabase db;	public StudentService(Context context) {				this.dbHelper = new DBOpenHelper(context);			}		public voID doInsert(Student stu){				db = dbHelper.getWritableDatabase();		db.execsql(sql_INSERT,new Object[]{stu.getStuname(),stu.getStuPwd()});			}		public voID updateStu(Student stu){				db = dbHelper.getWritableDatabase();		db.execsql(sql_UPDATE,stu.getStuPwd(),stu.getStuID()});			}		public voID delStu(Integer ID){				db = dbHelper.getWritableDatabase();		db.execsql(sql_DELETE,new Object[]{ID});			}		public Student LoadByID(Integer ID){				Student stu = new Student();		db = dbHelper.getReadableDatabase();		Cursor cursor = db.rawquery(sql_SELECT,new String[]{ID.toString()});		if(cursor.movetoNext()){			stu.setStuID(cursor.getInt(cursor.getColumnIndex("_ID")));			stu.setStuname(cursor.getString(cursor.getColumnIndex("stu_name")));			stu.setStuPwd(cursor.getString(cursor.getColumnIndex("stu_pwd")));		}		return stu;	}		public List<Student> LoadAll(Integer start,Integer end){				List<Student> stuList = new ArrayList<Student>();				db = dbHelper.getReadableDatabase();		Cursor cursor = db.rawquery(sql_LOAD,new String[]{start.toString(),end.toString()});				while(cursor.movetoNext()){						Student stu = new Student();			stu.setStuID(cursor.getInt(cursor.getColumnIndex("_ID")));			stu.setStuname(cursor.getString(cursor.getColumnIndex("stu_name")));			stu.setStuPwd(cursor.getString(cursor.getColumnIndex("stu_pwd")));			stuList.add(stu);					}				return stuList;			}		public Integer count(){				Integer count = 0;		db = dbHelper.getReadableDatabase();		Cursor cursor = db.rawquery(sql_COUNT,null);		cursor.movetoFirst();		count = cursor.getInt(0);		return count;			}}

3、sqlite事务 *** 作,通过db.beginTransaction();开启事务,db.setTransactionSuccessful();设置事务执行成功标志,最后再执行db.endTransaction();提交事务。

总结

以上是内存溢出为你收集整理的SQLite数据库学习笔记全部内容,希望文章能够帮你解决SQLite数据库学习笔记所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1180143.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存