android编程新手 - 创建数据库如果不存在

时间:2010-01-27 15:55:09

标签: android

我是Android的新手。

我想获得有关创建数据库(如果不存在)的最佳方法的信息,并使用内部联接查询管理少数表。

您有解释此主题的网页吗?

提前致谢。 问候。 何

2 个答案:

答案 0 :(得分:6)

  

我想了解相关信息   什么是最好的创建方式   数据库如果不存在,管理很少   具有内部联接查询的表。

使用SQLiteOpenHelper。它将帮助您在数据库不存在时创建数据库,并帮助您在架构更改时升级数据库。

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

答案 1 :(得分: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);
    }
}