本文共 13596 字,大约阅读时间需要 45 分钟。
使用 SQLiteDatabase 操作 SQLite 数据库 - public class DatabaseHelper extends SQLiteOpenHelper {
-
- private static final String name = "itcast";
- private static final int version = 1;
- public DatabaseHelper(Context context) {
-
- super(context, name, null, version);
- }
- @Override public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20), age INTEGER)");
- }
- @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(" ALTER TABLE person ADD phone VARCHAR(12) NULL ");
-
- }
- }
-
- package com.zyq.db;
- import android.app.Activity;
- import android.os.Bundle;
- public class MainActivity extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
- package com.zyq.db;
- import java.util.List;
- import android.test.AndroidTestCase;
- import android.util.Log;
- import com.zyq.service.DBOpenHelper;
- import com.zyq.service.PersonService;
- import com.zyq.voo.Person;
-
-
-
-
-
-
-
-
-
- public class PersonServiceTest extends AndroidTestCase
- {
- private final static String TAG="PersonServiceTest";
-
-
-
-
-
- public void testCreateDB() throws Throwable
- {
- DBOpenHelper dbOpenHelper=new DBOpenHelper(this.getContext());
- dbOpenHelper.getReadableDatabase();
- }
-
-
-
-
- public void testSave() throws Throwable
- {
- PersonService personService=new PersonService(this.getContext());
- personService.save(new Person("zhangsan","1360215320"));
- personService.save(new Person("lisi","1123"));
- personService.save(new Person("lili","232"));
- personService.save(new Person("wangda","123123"));
- personService.save(new Person("laozhu","234532"));
- }
-
-
-
-
- public void testFind() throws Throwable
- {
- PersonService personService=new PersonService(this.getContext());
- Person person=personService.find(1);
- Log.i(TAG,person.toString());
- }
-
-
-
-
- public void testUpdate() throws Throwable
- {
- PersonService personService=new PersonService(this.getContext());
- Person person=personService.find(1);
- person.setName("lisi");
- personService.update(person);
- }
-
-
-
-
- public void testGetCount() throws Throwable
- {
- PersonService personService=new PersonService(this.getContext());
- Log.i(TAG, personService.getCount()+"********");
- }
-
-
-
-
- public void testScroll() throws Throwable
- {
- PersonService personService=new PersonService(this.getContext());
- List<Person> persons=personService.getScrollData(3, 3);
- for(Person person:persons)
- {
- Log.i(TAG, person.toString());
- }
- }
-
-
-
-
- public void testDelete() throws Throwable
- {
- PersonService personService=new PersonService(this.getContext());
- personService.delete(5);
- }
- }
- package com.zyq.service;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DBOpenHelper extends SQLiteOpenHelper
- {
-
-
-
-
-
-
-
- public DBOpenHelper(Context context)
- {
- super(context, "zyq.db", null, 2);
- }
-
-
-
-
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))");
- }
-
-
-
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL ");
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.zyq.db"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <uses-library android:name="android.test.runner" />
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <instrumentation android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="com.zyq.db" android:label="Tests for My App" />
- </manifest>
- package com.zyq.service;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import com.zyq.voo.Person;
- public class PersonService
- {
- private DBOpenHelper helper;
- public PersonService(Context context)
- {
- helper=new DBOpenHelper(context);
- }
-
-
-
-
- public void save(Person person)
- {
- SQLiteDatabase db=helper.getWritableDatabase();
- db.execSQL("INSERT INTO person(name,phone) values(?,?)",new Object[]{person.getName().trim(),person.getPhone().trim()});
-
-
- }
-
-
-
-
- public void update(Person person)
- {
- SQLiteDatabase db=helper.getWritableDatabase();
- db.execSQL("update person set phone=?,name=? where personid=?",
- new Object[]{person.getPhone().trim(),person.getName().trim(),person.getId()});
- }
-
-
-
-
-
- public Person find(Integer id)
- {
- SQLiteDatabase db=helper.getReadableDatabase();
- Cursor cursor=db.rawQuery("select * from person where personid=?", new String[]{id.toString()});
- if(cursor.moveToFirst())
- {
- int personid=cursor.getInt(cursor.getColumnIndex("personid"));
- String name=cursor.getString(cursor.getColumnIndex("name"));
- String phone=cursor.getString(cursor.getColumnIndex("phone"));
-
- return new Person(personid,name,phone);
- }
- return null;
- }
-
-
-
-
- public void delete(Integer id)
- {
- SQLiteDatabase db=helper.getWritableDatabase();
- db.execSQL("delete from person where personid=?",
- new Object[]{id});
- }
-
-
-
-
-
- public long getCount()
- {
- SQLiteDatabase db=helper.getReadableDatabase();
- Cursor cursor=db.rawQuery("select count(*) from person", null);
- cursor.moveToFirst();
- return cursor.getLong(0);
- }
-
-
-
-
- public List<Person> getScrollData(int offset,int maxResult)
- {
- List<Person> persons=new ArrayList<Person>();
- SQLiteDatabase db=helper.getReadableDatabase();
- Cursor cursor=db.rawQuery("select * from person limit ?,?",
- new String[]{String.valueOf(offset),String.valueOf(maxResult)});
- while (cursor.moveToNext())
- {
- int personid=cursor.getInt(cursor.getColumnIndex("personid"));
- String name=cursor.getString(cursor.getColumnIndex("name"));
- String phone=cursor.getString(cursor.getColumnIndex("phone"));
-
- persons.add(new Person(personid,name,phone));
- }
-
- return persons;
- }
- }
- package com.zyq.voo;
- public class Person
- {
- private Integer id;
- private String name;
- private String phone;
-
- public Person(int personid, String name, String phone)
- {
- this.id=personid;
- this.name=name;
- this.phone=phone;
- }
-
- public Person(String name, String phone)
- {
- this.name = name;
- this.phone = phone;
- }
- public String toString()
- {
- return "Person [id=" + id + ", name=" + name + ", phone=" + phone + "]";
- }
- public Integer getId()
- {
- return id;
- }
- public void setId(Integer id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getPhone()
- {
- return phone;
- }
- public void setPhone(String phone)
- {
- this.phone = phone;
- }
-
-
-
- }
-
转载地址:http://ccyla.baihongyu.com/