SQLite 的应用实例

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', '[email protected]');");
            db.execSQL("INSERT INTO "
                    + _TABLE_NAME
                    + " (username, password, country, email)"
                    + " VALUES ('Doe', 'John', 'US', '[email protected]');");
            Cursor c = db.query("SELECT username,password" +
                    " FROM " + _TABLE_NAME
                    + " WHERE country = 'US' LIMIT 2;",
                    null);
           
            /* 只取username 咄1 附件:  

你可能感兴趣的:(C++,c,android,sqlite,OS)