简体   繁体   English

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

[英]New on android programming - create a database If it doesn't exist

I'm new here in Android. 我是Android新手。

I would like to have information about what is the best way to create a database if not exists, and manage few tables with inner join queries. 我想了解有关创建数据库(如果不存在)以及使用内部联接查询管理少量表的最佳方法的信息。

Do you have any web page explaining this subject? 您是否有任何网页说明此主题?

Thanks in advance. 提前致谢。 Regards. 问候。 Jose 约瑟

I would like to have information about what is the best way to create a database if not exists, and manage few tables with inner join queries. 我想了解有关创建数据库(如果不存在)以及使用内部联接查询管理少量表的最佳方法的信息。

Use SQLiteOpenHelper . 使用SQLiteOpenHelper It will help you create your database when the database does not exist and help you upgrade your database when your schema changes. 当数据库不存在时,它将帮助您创建数据库,并且在架构更改时,将帮助您升级数据库。

You can see example projects here and here that use SQLiteOpenHelper . 您可以在此处此处看到使用SQLiteOpenHelper示例项目。

DatabaseAdapter class helps to manage the tables of the databases: 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 class helps to create databases and tables: 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM