2015/10/19

Android 建立SQLite

Code:
package com.cyfang.npust.myclass;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {

 private final static String DATABASE_NAME = "npust.db";
 private static final int DATABASE_VERSION = 1;
 private static final String CREATE_DATABASE = "CREATE TABLE IF NOT EXISTS MyOptions("
   + "Name TEXT PRIMARY KEY NOT NULL," + "Doc TEXT NOT NULL);";

 public Database(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
  db.execSQL(CREATE_DATABASE);
 }

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

 }

}

//SQLiteDatabase db = new Database(context).getWritableDatabase();

Permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />