SQLite的学习(记事本的数据库增、删、改、查实现)

前端之家收集整理的这篇文章主要介绍了SQLite的学习(记事本的数据库增、删、改、查实现)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite是android自带的一个简化数据库sqlite是一个嵌入式的数据库引擎,专为资源有限的设备如(手机,PDA等)数据库提供存取。

首先,我们可以学习sqliteOpenHelper类,该类为android 提供了管理数据库的工具。

package com.ly.sqlite.db;

import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.database.sqlite.sqliteOpenHelper;

public class DBOpenHelper extends sqliteOpenHelper {

	private final String Creatsql =
			"create table note(_id integer primary key autoincrement,"
			+ "title varchar(40),"
			+ "content varchar(200))";
	public DBOpenHelper(Context context) {
		super(context,"note",null,1);
	}

	@Override
	public void onCreate(sqliteDatabase db) {
		db.execsql(Creatsql);
	}

	@Override
	public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
		System.out.println("-----onUpdate Called-----"
				+ oldVersion +"--->" + newVersion);
	}

}

这样我们就可以通过该类实现数据库的创建和变更,下面我们通过一个增、删、改、查实现数据库的操作
package com.ly.sqlite.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;

import com.ly.sqlite.bean.Note;

public class NoteDB {
	
	private DBOpenHelper dbOpenHelper;

	public NoteDB(Context context) {
		dbOpenHelper = new DBOpenHelper(context);
	}
	/**
	 * 保存数据
	 * @param note Note记事本对象
	 */
	public void save(Note note) {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execsql("insert into note(title,content) values(?,?)",new Object[]{note.getTitle(),note.getContent()});
		db.close();
	}
	/**
	 * 删除数据
	 * @param _id  Note编号 主键
	 */
	public void delete(int _id) {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execsql("delete from note where _id = ?",new Object[]{_id});
		db.close();
	}
	/**
	 * 更新数据
	 * @param note  Note记事本对象
	 */
	public void update(Note note) {
		sqliteDatabase db = dbOpenHelper.getWritableDatabase();
		db.execsql("update note set title = ?,content = ? where _id = ?",note.getContent(),note.get_id()});
		db.close();
	}
	/**
	 * 查找数据
	 * @param _id  Note编号 主键
	 * @return Note对象
	 */
	public Note find(int _id) {
		Note note = new Note();
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from note where _id = ?",new String[]{String.valueOf(_id)});
		if(cursor.moveToFirst()) {
			note.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
			note.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			note.setContent(cursor.getString(cursor.getColumnIndex("content")));
			db.close();
			//返回Note数据
			return note;
		}
		db.close();
		return null;
	}
	/**
	 * 查找数据
	 * @param offset 跳过对少条数据
	 * @param maxResult 最大查找数据条数
	 * @return List<Note>数据对象
	 */
	public List<Note> getScrollData(int offset,int maxResult) {
		List<Note> notes = new ArrayList<Note>();
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from note order by _id asc limit ?,?",new String[]{String.valueOf(offset),String.valueOf(maxResult)});
		while(cursor.moveToNext()) {
			int _id = cursor.getInt(cursor.getColumnIndex("_id"));
			String title = cursor.getString(cursor.getColumnIndex("title"));
			String content = cursor.getString(cursor.getColumnIndex("content"));
			notes.add(new Note(_id,title,content));
		}
		db.close();
		return notes;
	}
	/**
	 * 查找数据
	 * @param offset 跳过对少条数据
	 * @param maxResult 最大查找数据条数
	 * @return Cursor数据对象
	 */
	public Cursor getCursorScrollData(int offset,int maxResult) {
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from note order by _id asc limit ?,String.valueOf(maxResult)});
		db.close();
		return cursor;
	}
	/**
	 * 查询数据条数
	 * @return Int数据
	 */
	public long getCount() {
		long result = 0;
		sqliteDatabase db = dbOpenHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select count(*) as len from note",null);
		if(cursor.moveToFirst()) {
			result = cursor.getLong(cursor.getColumnIndex("len"));
		}
		db.close();
		return result;
	}
}

假如,我们想通过测试用例,可以配置AndroidManifest.xml实现,

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
         .....
        </activity>
        <uses-library android:name="android.test.runner"/>
    </application>
这样可以实现数据库的操作,对于不熟悉sql语句的人,我们还提供了inset等方法实现sqlite操作 原文链接:https://www.f2er.com/sqlite/200370.html

猜你在找的Sqlite相关文章