SQLite学习笔记

1:创建一个继承自SQLiteOpenHelper的DB.java

package com.example.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DB extends SQLiteOpenHelper {

	private String sql = "create table person("
			+"id integer primary key autoincrement,"
			+"name text,"
			+"age integer)" ;

	public DB(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}


}


MainActivity.java

package com.example.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private TextView text  ;
	private 	SQLiteDatabase database = null ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.text = (TextView)findViewById(R.id.text) ;
		SQLiteOpenHelper helper = new DB(this, "test.db", null, 1) ;
		database = helper.getWritableDatabase() ;
		//开始测试所写函数
		//首先放入五条数据
		for(int i = 0 ;i<5 ;i++) {
			ContentValues values = new ContentValues() ;
			values.put("name", "插入数据");
			values.put("age", i+20);
			insert("person",values) ;
		}
		
	delete("delete from person where age>23");    //将大于23岁的都删除
	Cursor cursor = query("select * from person") ;
	//showData(cursor);     //展示数据
	
	ContentValues values = new ContentValues() ;
	values.put("name", "测试update方法");
	values.put("age", 6666);
	update("person", values, "age=?",new String[]{"21"});  //将全部年龄为21的设置成6666
	showData(cursor);
	cursor.close();
	database.close();
    
		
	}
	/*
	 * @param tableName  表名称
	 * @param values   数据集
	 */
	private void insert(String tableName,ContentValues values) {
		if(database != null) {
			database.insert(tableName, null, values) ;
			//当然也可以借助database.exeSQL(sql语句执行)
		}else {
			Toast.makeText(this,"尚未实例化数据库对象!!", Toast.LENGTH_LONG).show(); 
		}
	}
	
	/*
	 * 根据SQL语句删除数据
	 */
	private void delete(String sql) {
		if(database != null) {
			database.execSQL(sql);
			//当然也可以借助database.exeSQL(sql语句执行)
		}else {
			Toast.makeText(this,"尚未实例化数据库对象!!", Toast.LENGTH_LONG).show(); 
		}
	}
	
	/*
	 * 查询语句,返回一个Cursor游标
	 */
	private Cursor query(String sql) {
			Cursor cursor = database.rawQuery(sql, null) ;
			return cursor ;		
	}
	
	
	/*
	 * 更改数据
	 */
	
	private void update(String table,ContentValues values,String whereClause,String[] whereArgs) {
		if(database != null) {
			database.update(table, values, whereClause, whereArgs) ;
			//当然也可以借助database.exeSQL(sql语句执行)
		}else {
			Toast.makeText(this,"尚未实例化数据库对象!!", Toast.LENGTH_LONG).show(); 
		}
	}
	/*
	 * 展示所有数据
	 */
	private void showData(Cursor cursor) {
		while(cursor.moveToNext()) {
			int nameColumn = cursor.getColumnIndex("name") ;
			int ageColumn = cursor.getColumnIndex("age") ;
			String name = cursor.getString(nameColumn) ;
			int age = cursor.getInt(ageColumn) ;
			this.text.append("名字:"+name+"  年龄:"+age+"\n");
		}
		this.text.append("所有数据查询完毕!");
	}
	
	
}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。