android 操作SQLite数据库

  • 内容
  • 评论
  • 相关

主程序文件

package com.gaoxueping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	private DBManager mgr;
	private ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.listView);
		mgr = new DBManager(this);

	}

	public void add(View view) {
		ArrayList<Person> persons = new ArrayList<Person>();

		Person person1 = new Person("Ella", 22, "lively girl");
		Person person2 = new Person("Jenny", 22, "beautiful girl");
		Person person3 = new Person("Jessica", 23, "sexy girl");
		Person person4 = new Person("Kelly", 23, "hot baby");
		Person person5 = new Person("Jane", 25, "a pretty woman");

		persons.add(person1);
		persons.add(person2);
		persons.add(person3);
		persons.add(person4);
		persons.add(person5);

		mgr.add(persons);
	}

	public void query(View view) {
		List<Person> persons = mgr.query();
		ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (Person person : persons) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", person.name);
			map.put("info", person.age + " years old, " + person.info);
			list.add(map);
		}
		SimpleAdapter adapter = new SimpleAdapter(this, list,
				android.R.layout.simple_list_item_2, new String[] { "name",
						"info" }, new int[] { android.R.id.text1,
						android.R.id.text2 });
		listView.setAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mgr.closeDB();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

DBManager.java

package com.gaoxueping;

import java.util.ArrayList;
import java.util.List;

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

public class DBManager {
	private DBHelper helper;
	private SQLiteDatabase db;
	
	public DBManager(Context context){
		helper = new DBHelper(context);
		db = helper.getWritableDatabase();
	}
	
	public void add(List<Person> persons){
		db.beginTransaction();
		try{
			for(Person person : persons){
				db.execSQL("INSERT INTO person VALUES(null, ?, ?, ?)", new Object[]{person.name, person.age, person.info});
			}
			db.setTransactionSuccessful();
		} finally{
			db.endTransaction();
		}
	}
	
	public List<Person> query(){
		ArrayList<Person> persons = new ArrayList<Person>();
		Cursor c = queryCursor();
		while(c.moveToNext()){
			Person person = new Person();
			person._id = c.getInt(c.getColumnIndex("_id"));
			person.name = c.getString(c.getColumnIndex("name"));
			person.age = c.getInt(c.getColumnIndex("age"));
			person.info = c.getString(c.getColumnIndex("info"));
			persons.add(person);
		}
		return persons;
	}
	
	public Cursor queryCursor(){
		Cursor c = db.rawQuery("SELECT * FROM person", null);
		return c;
	}
	
	public void closeDB(){
		db.close();
	}
}

DBHelper.java

package com.gaoxueping;

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 {
	private static final String DATABASE_NAME = "haha.db";
	private static final int DATABASE_VERSION = 1;
	
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE IF NOT EXISTS person" +  
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER, info TEXT)");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("ALTER TABLE person ADD COLUMN other STRING"); 

	}

}

Person.java

package com.gaoxueping;

public class Person {
	public int _id;
	public String name;
	public int age;
	public String info;
	
	public Person(){};
	
	public Person(String name, int age, String info){
		this.name = name;
		this.age = age;
		this.info = info;
	}
}

布局文件

<?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:text="add"
		android:onClick="add"/>
	<Button
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="update"
		android:onClick="update"/>
	<Button
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="delete"
		android:onClick="delete"/>
	<Button
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="query"
		android:onClick="query"/>
	<Button
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="queryTheCursor"
		android:onClick="queryTheCursor"/>
	<ListView
		android:id="@+id/listView"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
</LinearLayout>

完整项目下载地址
http://pan.baidu.com/s/1pJ146CV

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注