2010-01-27 71 views

回答

6

我想有有關 什麼是創建一個數據庫 如果不存在,最好的辦法,和管理幾個 表與內部聯接查詢。

使用SQLiteOpenHelper。它將幫助您在數據庫不存在時創建數據庫,並幫助您在模式更改時升級數據庫。

您可以看到使用SQLiteOpenHelper的示例項目herehere

0

DatabaseAdapter類可以幫助管理數據庫的表:

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 

public class DatabaseAdapter { 
    //Table name 
    private static final String LOGIN_TABLE = "login"; 
    //Table unique id 
    public static final String COL_ID = "id"; 
    //Table username and password columns 
    public static final String COL_USERNAME = "username"; 
    public static final String COL_PASSWORD = "password"; 

    private Context context; 
    private SQLiteDatabase database; 
    private DatabaseHelper dbHelper; 

    public DatabaseAdapter(Context context) { 
     this.context = context; 
    } 

    public DatabaseAdapter open() throws SQLException { 
     dbHelper = new DatabaseHelper(context); 
     database = dbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     dbHelper.close(); 
    } 

    public long createUser(String username, String password) { 
     ContentValues initialValues = createUserTableContentValues(username, password); 
     return database.insert(LOGIN_TABLE, null, initialValues); 
    } 

    public boolean deleteUser(long rowId) { 
     return database.delete(LOGIN_TABLE, COL_ID + "=" + rowId, null) > 0; 
    } 

    public boolean updateUserTable(long rowId, String username, String password) { 
     ContentValues updateValues = createUserTableContentValues(username, password); 
     return database.update(LOGIN_TABLE, updateValues, COL_ID + "=" + rowId, null) > 0; 
    } 

    public Cursor fetchAllUsers() { 
     return database.query(LOGIN_TABLE, new String[] { COL_ID, COL_USERNAME, 
        COL_PASSWORD }, null, null, null, null, null); 
    } 

    public Cursor fetchUser(String username, String password) { 
     Cursor myCursor = database.query(LOGIN_TABLE, 
           new String[] { COL_ID, COL_USERNAME, COL_PASSWORD }, 
              COL_USERNAME + "='" + username + "' AND " + 
              COL_PASSWORD + "='" + password + "'", 
              null, null, null, null); 

     if (myCursor != null) { 
      myCursor.moveToFirst(); 
     } 

     return myCursor; 
    } 

    public Cursor fetchUserById(long rowId) throws SQLException { 
     Cursor myCursor = database.query(LOGIN_TABLE, 
          new String[] { COL_ID, COL_USERNAME, COL_PASSWORD }, 
          COL_ID + "=" + rowId, null, null, null, null); 
     if (myCursor != null) { 
      myCursor.moveToFirst(); 
     } 
     return myCursor; 
    } 

    private ContentValues createUserTableContentValues(String username, String password) { 
     ContentValues values = new ContentValues(); 
     values.put(COL_USERNAME, username); 
     values.put(COL_PASSWORD, password); 
     return values; 
    } 
} 

DatabaseHelper類可幫助創建數據庫和表:

package com.example.possibleinventory.database; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
/** 
* This class creates the relation with the SQLite Database Helper 
* through which queries can be SQL called.   
* @author Andrei 
* 
*/ 
public class DatabaseHelper extends SQLiteOpenHelper { 
    // The database name and version 
    private static final String DB_NAME = "inventorymanagement"; 
    private static final int DB_VERSION = 1; 
    // The database user table 
    private static final String DB_TABLE = "create table login (id integer primary key autoincrement, " 
              + "username text not null, password text not null);"; 
    /** 
    * Database Helper constructor. 
    * @param context 
    */ 
    public DatabaseHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
    } 
    /** 
    * Creates the database tables. 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase database) { 
     database.execSQL(DB_TABLE); 
    } 
    /** 
    * Handles the table version and the drop of a table. 
    */   
    @Override 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
     Log.w(DatabaseHelper.class.getName(), 
       "Upgrading databse from version" + oldVersion + "to " 
       + newVersion + ", which will destroy all old data"); 
     database.execSQL("DROP TABLE IF EXISTS user"); 
     onCreate(database); 
    } 
} 
相關問題