android中关于sqlite的简单运用

.sqlite是android内置的轻量级数据库,功能强大。

先上一个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">
	<Button android:layout_width="match_parent"
		android:layout_height="wrap_content" android:id="@+id/button1"
		android:text="添加" />
	<Button android:text="删除" android:id="@+id/button2"
		android:layout_width="match_parent" android:layout_height="wrap_content" />
	<Button android:text="创建" android:id="@+id/button3"
		android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
	<Button android:text="查询数据" android:id="@+id/button4"
		android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
	<Button android:text="删除数据库" android:id="@+id/button5"
		android:layout_width="match_parent" android:layout_height="wrap_content"></Button>
	<TextView android:text="状态信息" android:layout_width="wrap_content"
		android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1"
		android:layout_height="wrap_content"></TextView>

</LinearLayout>

需要两个java文件

DBHelper.java

package com.fover;

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

public class DBHelper extends SQLiteOpenHelper {

	// 继承了SQLiteOpenHelper之后的构造函数
	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 当数据库被创建时执行的代码
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// 当数据库升级时执行的代码
	}

}
NotebookActivity.java

package com.fover;

import java.util.Random;

import android.app.Activity;
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;
import android.widget.TextView;

public class NotebookActivity extends Activity implements OnClickListener {

	private Button addButton;
	private Button delButton;
	private Button createButton;

	private DBHelper dbHelper;
	private TextView infoTextView;
	private Button dbQuerry;
	private Button dbDel;
	private Cursor cursor;

	private final static String DATEBASE_NAME = "fover.db";
	private final static int DATEBASE_VERSION = 1;
	private final static String TABLE_NAME = "employee";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 要先把DBHelper通过new方法new出来才能对数据库进行操作 ,否则会报错哦
		dbHelper = new DBHelper(this, DATEBASE_NAME, null, DATEBASE_VERSION);

		// 取得按钮控件设置标签和监听
		addButton = (Button) findViewById(R.id.button1);
		addButton.setOnClickListener(this);
		delButton = (Button) findViewById(R.id.button2);
		delButton.setOnClickListener(this);
		createButton = (Button) findViewById(R.id.button3);
		createButton.setOnClickListener(this);
		infoTextView = (TextView) findViewById(R.id.textView1);
		dbQuerry = (Button) findViewById(R.id.button4);
		dbQuerry.setOnClickListener(this);
		dbDel = (Button) findViewById(R.id.button5);
		dbDel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 添加数据
		case R.id.button1:
			dbAdd();
			break;
		// 删除数据
		case R.id.button2:
			dbDel();
			break;
		// 创建数据库
		case R.id.button3:
			dbCreate();
			break;
		// 查询数据
		case R.id.button4:
			dbQuerry();
			break;
		// 删除数据库
		case R.id.button5:
			delDB();
			break;
		default:
			break;
		}
	}

	// 从字面上就能够理解 我就不做过多说明了
	private void delDB() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
		try {
			db.execSQL(sql);
			infoTextView.setText("删除数据库成功");
		} catch (Exception e) {
			infoTextView.setText("Fail" + e.toString());
		}

	}

	private void dbQuerry() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		try {
			String[] column = { "id", "name", "age" };
			cursor = db.query(TABLE_NAME, column, null, null, null,
					null, null);
			int num = cursor.getCount();
			infoTextView.setText("共有" + Integer.toString(num) + "条记录");
		} catch (Exception e) {
			infoTextView.setText("读取数据失败" + e.toString());
		}finally{
			if(cursor!=null){
				cursor.close();
			}
		}
	}

	private void dbCreate() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ " (ID INTEGER PRIMARY KEY, Name VARCHAR, Age INTEGER);";
		try {
			db.execSQL(sql);
			infoTextView.setText("数据表成功创建");
		} catch (Exception e) {
			infoTextView.setText("创建失败!");
		}
	}

	private void dbDel() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try {
			db.delete(TABLE_NAME, "id=1", null);
			infoTextView.setText("删除记录成功");
		} catch (Exception e) {
			infoTextView.setText("失败" + e.toString());
		}
	}

	private void dbAdd() {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		try {
			Random random = new Random();
			for (int i = 0; i < 5; i++) {
				String sql = "insert into " + TABLE_NAME
						+ " (name, age) values ('name" + String.valueOf(i)
						+ "', " + random.nextInt() + ")";
				db.execSQL(sql);
			}
			infoTextView.setText("成功添加五条记录");
		} catch (Exception e) {
			infoTextView.setText("记录添加失败" + e.toString());
		}
	}

}



代码比较粗糙,请见谅!

运行截图:



发布了24 篇原创文章 · 获赞 5 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章