Android SQLite数据操作 【学习记录】

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"
    >
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/create"
    android:text="创建数据库"
    />
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/upgrade"
    android:text="更新数据库"
    />
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/insert"
    android:text="插入数据"
    />
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/update"
    android:text="修改数据"
    />
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/select"
    android:text="查询数据"
    />
</LinearLayout>

继承自SQLiteOpenHelper的MyOpenHleper类:
package com.android.danny.hleper;

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

public class MyOpenHleper extends SQLiteOpenHelper {

	public static final int VERSION =1;
	
	public MyOpenHleper(Context context, String name) {
		super(context, name, null, VERSION);
		// TODO Auto-generated constructor stub
	}
	
	public MyOpenHleper(Context context, String name,int ver) {
		super(context, name, null, ver);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		System.out.println("------Create Database----");
		db.execSQL("CREATE TABLE person (id int ,name varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		System.out.println("------Upgrade Database----");
	}

}

SQLite数据操作类:
package com.android.danny.db;

import com.android.danny.hleper.MyOpenHleper;

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 Sqlite3 extends Activity implements OnClickListener{
    
	Button create;
	Button upgrade;
	Button insert;
	Button update;
	Button select;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        create = (Button)findViewById(R.id.create);
        upgrade = (Button)findViewById(R.id.upgrade);
        insert = (Button)findViewById(R.id.insert);
        update = (Button)findViewById(R.id.update);
        select = (Button)findViewById(R.id.select);
        
        create.setOnClickListener(this);
        upgrade.setOnClickListener(this);
        insert.setOnClickListener(this);
        update.setOnClickListener(this);
        select.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		MyOpenHleper dbDatabase;
		if (v == create) {
			dbDatabase = new MyOpenHleper(this, "test_db.db");
			//创建数据库
			dbDatabase.getWritableDatabase();
			
		}else if (v == upgrade) {
			//更新数据库
			dbDatabase = new MyOpenHleper(this, "test_db.db",2);
			dbDatabase.getReadableDatabase();
			
		}else if (v == insert) {
			//插入数据
			ContentValues values = new ContentValues();
			values.put("id", 1);
			values.put("name", "danny");
			dbDatabase = new MyOpenHleper(this, "test_db.db");
			SQLiteDatabase database = dbDatabase.getWritableDatabase();
			database.insert("person", null, values);
			
		}else if (v == update) {
			//修改数据
			ContentValues values = new ContentValues();
			values.put("name", "danny&kiki");
			dbDatabase = new MyOpenHleper(this, "test_db.db");
			SQLiteDatabase database = dbDatabase.getWritableDatabase();
			database.update("person", values, "id=?", new String[] {"1"});
			
		}else if (v == select) {
			//数据查询操作
			dbDatabase = new MyOpenHleper(this, "test_db.db");
			SQLiteDatabase database = dbDatabase.getReadableDatabase();
			Cursor cursor = database.query("person", new String[] {"id","name"},
					"id=?", new String[] {"1"}, 
					null, null, null);
			while (cursor.moveToNext()) {
				String name = cursor.getString(cursor.getColumnIndex("name"));
				System.out.println("query:----->"+name);
			}
		}
		
	}
}

你可能感兴趣的:(数据库,android,sqlite)