Android数据存储之SQLite

这一节比较重要,SQLite是一个轻量级的数据库。

它的功能叶很全,里面的各种数据库语句也一样。

在Android都应用中都使用了SQLite数据库,比如:电话本。

 

Activity:

 

package com.ko8e;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SQLiteActivity extends Activity {
	/** Called when the activity is first created. */
	private Button button1 = null;
	private Button button2 = null;
	private Button button3 = null;
	private Button button4 = null;
	private Button button5 = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		button5 = (Button) findViewById(R.id.button5);

		button1.setOnClickListener(new CreateListener());
		button2.setOnClickListener(new UpdateRecordListener());
		button3.setOnClickListener(new InsertListener());
		button4.setOnClickListener(new QueryListener());
		button5.setOnClickListener(new UpdateListener());

	}

	private class CreateListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db");
			SQLiteDatabase sqld = db.getReadableDatabase();
		}

	}

	private class UpdateRecordListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db");
			SQLiteDatabase sqld = db.getReadableDatabase();
			ContentValues values = new ContentValues();
			values.put("name", "kobe bryant");
			sqld.update("user", values, "id=?", new String[]{"1"});
		}

	}

	private class InsertListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			ContentValues values = new ContentValues();
			values.put("id", 1);
			values.put("name", "ko8e");
			DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db", 2);
			SQLiteDatabase sqld = db.getReadableDatabase();
			sqld.insert("user", null, values);
		}

	}

	private class QueryListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db");
			SQLiteDatabase sqld = db.getReadableDatabase();
			Cursor cursor = sqld.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
			while(cursor.moveToNext()) {
				String name = cursor.getString(cursor.getColumnIndex("name"));
				System.out.println("查询: " + name);
			}
		}

	}

	private class UpdateListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			DBhelper db = new DBhelper(SQLiteActivity.this, "ko8e_db", 2);
			SQLiteDatabase sqld = db.getReadableDatabase();
		}

	}
}

 

封装类:

package com.ko8e;

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

public class DBhelper extends SQLiteOpenHelper{

	private static final int VERSION = 1;
	
	public DBhelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public DBhelper(Context context, String name) {
		this(context, name, VERSION);
	}
	
	public DBhelper(Context context, String name, int version) {
		this(context, name, null, version);
	}
	
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		System.out.println("create a DB");
		arg0.execSQL("create table user(id int, name varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		System.out.println("update a DB");
	}

}

 

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
	android:id="@+id/button1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/create"
	/>    
<Button
	android:id="@+id/button2"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/add"
	/>  
<Button
	android:id="@+id/button3"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/insert"
	/>  
<Button
	android:id="@+id/button4"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/query"
	/>  
<Button
	android:id="@+id/button5"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/update"
	/>  				
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值