android sqlite应用

 

package com.action;

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

public class SqliteActivity extends Activity {

	private Button upgreateDatabase;
	private Button insert;
	private Button delete;
	private Button update;
	private Button query;
	private TextView tv;
	private SQLiteDatabase sqlDb;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		upgreateDatabase = (Button) findViewById(R.id.upgreateDatabase);
		insert = (Button) findViewById(R.id.insert);
		delete = (Button) findViewById(R.id.delete);
		update = (Button) findViewById(R.id.update);
		query = (Button) findViewById(R.id.query);
		tv = (TextView) findViewById(R.id.tv);

		upgreateDatabase.setOnClickListener(new ButtonListener());
		insert.setOnClickListener(new ButtonListener());
		delete.setOnClickListener(new ButtonListener());
		update.setOnClickListener(new ButtonListener());
		query.setOnClickListener(new ButtonListener());
		
		/*
		 * 第二个参数为你要创建的数据库名,而不是表名;
		 * 数据库是在运行getWritableDatabase()或getReadeDatabase()时创建,而不是现在;
		 * 最后一个参数为版本号,版本号不得小于1,不然会报错。
		 */
		MyHelper myHelper = new MyHelper(SqliteActivity.this,
				"test_user", null, 1);
		//创建数据库与并执行onCreate(SQLiteDatabase db)函数,获得数据库的操作权
		sqlDb = myHelper.getWritableDatabase();
	}

	class ButtonListener implements android.view.View.OnClickListener {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.upgreateDatabase:

				break;
			case R.id.insert:
				ContentValues values = new ContentValues();
				values.put("name", "hilary");
				values.put("age", 22);
				values.put("sex", "men");
				values.put("phone", 134343241);
				sqlDb.insert("user", null, values);
				break;
			case R.id.delete:

				break;
			case R.id.update:
				ContentValues values1 = new ContentValues();
				values1.put("id", 1);
				values1.put("name", "ck");
				sqlDb.update("user", values1, "id=?", new String[] { "1" });
				break;
			case R.id.query:
				StringBuffer buff = new StringBuffer();
				Cursor cursor = sqlDb.query("user",
						new String[] { "id", "name" ,"age", "sex", "phone"}, null,
						null, null, null, null);
				while(cursor.moveToNext()){
					buff.append(cursor.getString(cursor.getColumnIndex("id"))+",");
					buff.append(cursor.getString(cursor.getColumnIndex("name"))+",");
					buff.append(cursor.getInt(cursor.getColumnIndex("age"))+",");
					buff.append(cursor.getString(cursor.getColumnIndex("sex"))+",");
					buff.append(cursor.getString(cursor.getColumnIndex("phone"))+"\n"+"\n");
				}
				System.out.println(buff.toString());
				tv.setText(buff.toString());
				break;
			}
		}
	}
}

 我们可以在Doc下找看我们所创建的数据库及表的信息,见关于SQlite操作命令文章

你可能感兴趣的:(android,sqlite,OS)