Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package umn.ti;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- public class DBAdapter {
- public static final String KEY_USERNAME = "username";
- public static final String KEY_PASSWORD = "password";
- private static final String TAG = "DBAdapter";
- private static final String DATABASE_NAME = "UMN";
- private static final String DATABASE_TABLE = "accounts";
- private static final int DATABASE_VERSION = 2;
- private static final String DATABASE_CREATE =
- "create table accounts (username varchar(100) primary key, "
- + "password varchar(100) not null);";
- private final Context context;
- private DatabaseHelper DBHelper;
- private SQLiteDatabase db;
- public DBAdapter(Context ctx)
- {
- this.context = ctx;
- DBHelper = new DatabaseHelper(context);
- }
- private static class DatabaseHelper extends SQLiteOpenHelper
- {
- DatabaseHelper(Context context)
- {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- try {
- db.execSQL(DATABASE_CREATE);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS contacts");
- onCreate(db);
- }
- }
- //---opens the database---
- public DBAdapter open() throws SQLException
- {
- db = DBHelper.getWritableDatabase();
- return this;
- }
- //---closes the database---
- public void close()
- {
- DBHelper.close();
- }
- //---insert a contact into the database---
- public long insertAccount(String username, String password)
- {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_USERNAME, username);
- initialValues.put(KEY_PASSWORD, password);
- return db.insert(DATABASE_TABLE, null, initialValues);
- }
- }
Add Comment
Please, Sign In to add comment