android中的sqlite實現程式

來源:互聯網
上載者:User

建立項目Events,項目的主程式檔案有:Events.java、EventsData.java、Constants.java;布局檔案有item.xml。

Events.java檔案是項目的入口檔案,大致如下:

 代碼如下 複製代碼

package org.hxsd.events;
 
import static android.provider.BaseColumns._ID;
import static org.hxsd.events.Constants.TABLE_NAME;
import static org.hxsd.events.Constants.TIME;
import static org.hxsd.events.Constants.TITLE;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
 
public class Events extends ListActivity {
 private EventsData events;
 //查詢的欄位
 private static String[] FROM = {_ID, TIME, TITLE};
 //排序
 private static String ORDER_BY = TIME + " DESC";
 //顯示
 private static int[] TO = {R.id.rowid, R.id.time, R.id.title};
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        events = new EventsData(this);
        try{
         //添加記錄
         addEvent("Hello, Android!");
         //獲得記錄資訊
         Cursor cursor = getEvents();
         //顯示記錄資訊
         showEvents(cursor);
        }finally{
         //關閉資料庫
         events.close();
        }
    }
 
    /**
     * 添加資料
     * @param string
     */
    private void addEvent(String string){
     SQLiteDatabase db = events.getWritableDatabase();
     ContentValues values = new ContentValues();
     values.put(TIME, System.currentTimeMillis());
     values.put(TITLE, string);
     db.insertOrThrow(TABLE_NAME, null, values);
    }
 
    /**
     * 獲得記錄
     * @return
     */
    private Cursor getEvents(){
     SQLiteDatabase db = events.getReadableDatabase();
     Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
     startManagingCursor(cursor);
     return cursor;
    }
 
    /**
     * 顯示記錄
     * @param cursor
     */
    private void showEvents(Cursor cursor){
     /*StringBuilder builder = new StringBuilder("Saved events:n");
     while(cursor.moveToNext()){
      long id = cursor.getLong(0);
      long time = cursor.getLong(1);
      String title = cursor.getString(2);
      builder.append(id).append(": ");
      builder.append(time).append(": ");
      builder.append(title).append("n");
     }
     TextView textView = (TextView) findViewById(R.id.TextView01);
     textView.setText(builder);*/
 
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
     setListAdapter(adapter);
    }
}
EventsData.java檔案大致如下:

package org.hxsd.events;
 
import static android.provider.BaseColumns._ID;
import static org.hxsd.events.Constants.TABLE_NAME;
import static org.hxsd.events.Constants.TIME;
import static org.hxsd.events.Constants.TITLE;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
 
public class EventsData extends SQLiteOpenHelper{
 //資料庫名稱
 private static final String DATABASE_NAME = "events.db";
 //資料庫版本
 private static final int DATABASE_VERSION = 2;
 
 //建構函式
 public EventsData(Context ctx){
  super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
 }
 
 @Override
 public void onCreate(SQLiteDatabase db) {
  // 建立資料表events
  db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + TIME + " INTEGER," + TITLE + " TEXT NOT NULL)");
 }
 
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  //如果版本有改動,刪除表events然後建立
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  onCreate(db);
 }
 
}

Constants.java檔案大致如下:

 

 代碼如下 複製代碼

package org.hxsd.events;
 
import android.provider.BaseColumns;
 
public interface Constants extends BaseColumns{
 //表名稱
   public static final String TABLE_NAME = "events";
   //欄位名稱
   public static final String TIME = "time";
   public static final String TITLE = "title";
}
item.xml檔案為布局檔案,位於res檔案夾下的layout下面,大致如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:orientation="horizontal"
    >
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rowid"
    android:paddingRight="40dip"/>
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/time"
    android:layout_toRightOf="@id/rowid"
    android:paddingRight="40dip"/>
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:layout_toRightOf="@id/time"
    android:paddingRight="40dip"/>
</RelativeLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.