sqlite
步骤:
1. create a database
2. open the database
3. create a table
4. create an insert interface for data sets
5. create a query interface for data sets
6. close the database
下面以添加书籍为例:
book.java
<span style="font-size:24px;">package com.raise.entity; import java.util.Date; public class Book { private int id; private String title; private double price; private Date pubDate; public Book() { super(); } public Book(int id, String title, double price, Date pubDate) { super(); this.id = id; this.title = title; this.price = price; this.pubDate = pubDate; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { if (price < 0) { throw new IllegalArgumentException("price must be positive"); } this.price = price; } public Date getPubDate() { return pubDate; } public void setPubDate(Date pubDate) { Date today = new Date(); if (pubDate.after(today)) { throw new IllegalArgumentException("pub date cannot be greater than today"); } this.pubDate = pubDate; } } </span>
<span style="font-size:24px;">package com.raise.database; import com.raise.entity.BooksDbConstants; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import static com.raise.entity.BooksDbConstants.*; public class BooksDbHelper extends SQLiteOpenHelper{ public BooksDbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public BooksDbHelper(Context context, String name) { this(context, name, null, BooksDbConstants.DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub Log.d(LOG_TAG, "create all the tables"); String sql = "create table "+TABLE_NAME +"("+BOOK_ID+" integer primary key," +BOOK_TITLE+" text," +BOOK_PRICE+" real," +BOOK_PUBDATE+" integer" +")"; try { db.execSQL(sql); } catch (Exception e) { // TODO: handle exception Log.d(LOG_TAG, "create table exception:"+e.getMessage()); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } </span>
<span style="font-size:24px;">package com.raise.database; import com.raise.entity.BooksDbConstants; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import static com.raise.entity.BooksDbConstants.*; public class BooksDbHelper extends SQLiteOpenHelper{ public BooksDbHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public BooksDbHelper(Context context, String name) { this(context, name, null, BooksDbConstants.DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub Log.d(LOG_TAG, "create all the tables"); String sql = "create table "+TABLE_NAME +"("+BOOK_ID+" integer primary key," +BOOK_TITLE+" text," +BOOK_PRICE+" real," +BOOK_PUBDATE+" integer" +")"; try { db.execSQL(sql); } catch (Exception e) { // TODO: handle exception Log.d(LOG_TAG, "create table exception:"+e.getMessage()); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } </span>
<span style="font-size:24px;">package com.raise.sqlite_books; import java.util.Date; import com.raise.database.BooksDbHelper; import com.raise.entity.BooksDbConstants; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.content.ContentValues; import android.database.Cursor; import android.database.CursorJoiner; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.os.Build; import static com.raise.entity.BooksDbConstants.*; public class MainActivity extends Activity { Button cteatebt; Button insertbt; Button getdatabt; Button updatabt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建表按钮 cteatebt = (Button) findViewById(R.id.cteateButton); cteatebt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub BooksDbHelper dbHelper = new BooksDbHelper(MainActivity.this, BooksDbConstants.DATABASE_NAME); dbHelper.getReadableDatabase(); } }); //插入一条数据按钮 insertbt = (Button) findViewById(R.id.insert); insertbt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(BooksDbConstants.LOG_TAG,"------insert"); BooksDbHelper dbHelper = new BooksDbHelper(MainActivity.this, BooksDbConstants.DATABASE_NAME); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues newbookValues = new ContentValues(); newbookValues.put(BooksDbConstants.BOOK_ID,"1"); newbookValues.put(BooksDbConstants.BOOK_TITLE,"Android指南"); newbookValues.put(BooksDbConstants.BOOK_PRICE,56.5); newbookValues.put(BooksDbConstants.BOOK_PUBDATE,new Date().getTime()); try { db.insert(BooksDbConstants.TABLE_NAME, null, newbookValues); Log.d(BooksDbConstants.LOG_TAG,"insert success"); } catch (Exception e) { // TODO: handle exception Log.e(BooksDbConstants.LOG_TAG,e.getMessage()); } } }); //获取数据按钮 getdatabt = (Button) findViewById(R.id.getdata); getdatabt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub BooksDbHelper dbHelper = new BooksDbHelper(MainActivity.this, BooksDbConstants.DATABASE_NAME); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query(BooksDbConstants.TABLE_NAME, new String[]{BOOK_ID,BOOK_TITLE,BOOK_PRICE,BOOK_PUBDATE}, BOOK_ID+"=?", new String[]{1+""}, null, null, null); while(cursor.moveToNext()){ String title = cursor.getString(cursor.getColumnIndex(BOOK_TITLE)); Log.d(LOG_TAG,"查找到的title:---->"+title); String price = cursor.getString(cursor.getColumnIndex(BOOK_PRICE)); Log.d(LOG_TAG,"查找到的title:---->"+price); } } }); //更新按钮 updatabt = (Button) findViewById(R.id.updata); updatabt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub BooksDbHelper dbHelper = new BooksDbHelper(MainActivity.this, BooksDbConstants.DATABASE_NAME); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues newbookValues = new ContentValues(); newbookValues.put(BOOK_PRICE, 99.9); db.update(TABLE_NAME, newbookValues, BOOK_ID+"=?", new String[]{1+""}); Log.d(LOG_TAG,"updata success"); } }); } } </span>