android编程的新功能-如果数据库不存在,则创建数据库

android编程的新功能-如果数据库不存在,则创建数据库,android,Android,我是新来安卓的 如果不存在数据库,我想知道创建数据库的最佳方法是什么,并使用内部联接查询管理几个表 你有任何解释这个主题的网页吗 提前谢谢。 当做 何塞 我想了解一下 创建一个 数据库(如果不存在),并管理少量 具有内部联接查询的表 使用SQLiteOpenHelper。它将帮助您在数据库不存在时创建数据库,并在模式更改时帮助您升级数据库 您可以看到使用SQLiteOpenHelper的示例项目和实例 我想了解一下 创建一个 数据库(如果不存在),并管理少量 具有内部联接查询的表 使用SQLit

我是新来安卓的

如果不存在数据库,我想知道创建数据库的最佳方法是什么,并使用内部联接查询管理几个表

你有任何解释这个主题的网页吗

提前谢谢。 当做 何塞

我想了解一下 创建一个 数据库(如果不存在),并管理少量 具有内部联接查询的表

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

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

我想了解一下 创建一个 数据库(如果不存在),并管理少量 具有内部联接查询的表

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

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

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);
    }
}
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);
    }
}