WEB开发网
开发学院手机开发Android 开发 在 Android 里使用数据库 阅读

在 Android 里使用数据库

 2010-02-24 04:45:00 来源:WEB开发网   
核心提示:import android.app.Activity;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQ

import android.app.Activity;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.os.Bundle;

import android.provider.BaseColumns;

import android.widget.TextView;

public class AaBbActivity extends Activity {

/** Called when the activity is first created. 中 */

private static final String DATABASE_NAME = "con_tact.db";

private static final int DATABASE_VERSION = 2;

private static final String CONT_TABLE_NAME = "cont";

private static final String COL_NAME = "name";

private static final String COL_TEL = "tel";

private static class DatabaseHelper extends SQLiteOpenHelper {

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + CONT_TABLE_NAME + " ("

+ BaseColumns._ID + " INTEGER PRIMARY KEY,"

+ COL_NAME + " TEXT,"

+ COL_TEL + " TEXT"

+ ");");

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// Log.w(TAG, "Upgrading database from version " + oldVersion + " to "

// + newVersion + ", which will destroy all old data");

db.execSQL("DROP TABLE IF EXISTS notes");

onCreate(db);

}

}

private DatabaseHelper mOpenHelper;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tv = new TextView(this); // 定义一个TextView用来显示到屏幕上

setContentView(tv);

1 2  下一页

Tags:Android 使用 数据库

编辑录入:coldstar [复制链接] [打 印]
赞助商链接