论坛首页 移动开发技术论坛

SQLite 的应用实例

浏览 4603 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-02-17  
package com.gggeye;


import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;

 

public class DataTest extends ListActivity {
    private final String _DB_NAME = "userdb";
    private final String _TABLE_NAME = "users";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        ArrayList<String> results = new ArrayList<String>();
        SQLiteDatabase db = null;
        try
        {
            //Log.log("中国");
            /* 新建 database */
            this.createDatabase(_DB_NAME, 1, MODE_PRIVATE, null);
            /* 打开 database */
            db = this.openDatabase(_DB_NAME, null);
            /* 新建 table */
            /* Create a Table in the Database. */
            db.execSQL("CREATE TABLE IF NOT EXISTS "
                                + _TABLE_NAME
                                + " (username VARCHAR, password VARCHAR,"
                                + " country VARCHAR, email VARCHAR);");
            /* 新建两笔资料 */
            db.execSQL("INSERT INTO "
                    + _TABLE_NAME
                    + " (username, password, country, email)"
                    + " VALUES ('Gramlich', 'Nicolas', 'Germany', 'Gramlich@mymobisoft.com');");
            db.execSQL("INSERT INTO "
                    + _TABLE_NAME
                    + " (username, password, country, email)"
                    + " VALUES ('Doe', 'John', 'US', 'Doe@mymobisoft.com');");
            Cursor c = db.query("SELECT username,password" +
                    " FROM " + _TABLE_NAME
                    + " WHERE country = 'US' LIMIT 2;",
                    null);
           
            /* 只取username 咄1 附件:  
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics