SQLite 的应用实例

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', '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);

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页