开发者

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

开发者 https://www.devze.com 2022-12-18 10:21 出处:网络
I\'m new here in 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.

I'm new here in 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.开发者_StackOverflow社区 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. 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.


DatabaseAdapter class helps to manage the tables of the databases:

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:

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

精彩评论

暂无评论...
验证码 换一张
取 消