Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.alannah.overwatch;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import android.widget.Toast;
- public class MyDBManager
- {
- final String TAG = MyDBManager.class.getSimpleName();
- // These are the names of the columns the table will contain
- public static final String KEY_ROW_ID = "_id";
- public static final String KEY_HERO_NAME = "hero_name";
- public static final String KEY_HERO_DESC = "hero_desc";
- public static final String KEY_HERO_ABILITIES = "hero_abilities";
- private static final String DATABASE_NAME = "Heroes";
- private static final String DATABASE_TABLE = "Descriptions";
- private static final int DATABASE_VERSION = 2;
- // This is the string containing the SQL database create statement
- private static final String DATABASE_CREATE =
- "create table " + DATABASE_TABLE +
- " (" + KEY_ROW_ID + " integer primary key autoincrement, " +
- KEY_HERO_NAME + " text not null, " +
- KEY_HERO_DESC + " text not null, " +
- KEY_HERO_ABILITIES + " integer not null);";
- public static final String KEY_USERNAME = "username";
- public static final String KEY_PASSWORD = "password";
- private static final String DATABASE_TABLE_USERS = " users";
- private static final String DATABASE_CREATE_USERS =
- "create table " + DATABASE_TABLE_USERS +
- " (" + KEY_ROW_ID + " integer primary key autoincrement, "
- +KEY_USERNAME + " text not null, " +
- KEY_PASSWORD + " text not null);";
- private final Context context;
- private DatabaseHelper DBHelper;
- private SQLiteDatabase db;
- // constructor for your class
- public MyDBManager(Context ctx) {
- // Context is a way that Android transfers info about Activities and apps.
- this.context = ctx;
- DBHelper = new DatabaseHelper(context);
- }
- private static class DatabaseHelper extends SQLiteOpenHelper {
- // constructor for your dB helper class. This code is standard. You’ve set up the parameter values for the constructor already...database name,etc
- DatabaseHelper(Context context) {
- // This is the helper class that will create the dB if it doesn’t exist and upgrades it if
- // the structure has changed. It needs a constructor, an onCreate() method and an onUpgrade() method
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- // end of the help class
- @Override
- public void onCreate(SQLiteDatabase db) {
- // The “Database_create” string below needs to contain the SQL statement needed to create the dB
- // db.delete(DATABASE_TABLE,null,null);
- db.execSQL(DATABASE_CREATE);
- db.execSQL(DATABASE_CREATE_USERS);
- db.execSQL("insert into" +DATABASE_TABLE_USERS+ "("+KEY_USERNAME+", "+KEY_PASSWORD+") values('[admin]', '[admin]')");
- db.execSQL("insert into" +DATABASE_TABLE_USERS+ "("+KEY_USERNAME+", "+KEY_PASSWORD+") values('[admin2]', '[admin2]')");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // db.delete(DATABASE_TABLE,null,null);
- db.execSQL(DATABASE_CREATE);
- db.execSQL(DATABASE_CREATE_USERS);
- }
- }
- // If you want to change the structure of your database, e.g. Add a new column to a table,
- // the code will go head..
- //This method only triggers if the database version number has increased
- // from here on, include whatever methods will be used to access or change data in the database
- //---opens the database--- any activity that uses the dB will need to do this
- public MyDBManager open()
- {
- try{
- db = DBHelper.getWritableDatabase();
- }catch(SQLException e) {
- Log.e("error", "Failed to open database: " + e);
- }
- return this;
- }
- //---closes the database--- any activity that uses the dB will need to do this
- public void close()
- {
- DBHelper.close();
- }
- //---insert a hero into the database---
- public long insertHero(String heroName, String heroDesc, String heroAbilities) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_HERO_NAME, heroName);
- initialValues.put(KEY_HERO_DESC, heroDesc);
- initialValues.put(KEY_HERO_ABILITIES, heroAbilities);
- return db.insert(DATABASE_TABLE, null, initialValues);
- }
- //---insert a user into the database---
- public long insertUser(String username, String password) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_USERNAME, username);
- initialValues.put(KEY_PASSWORD, password);
- return db.insert(DATABASE_TABLE_USERS, null, initialValues);
- }
- //---retrieves all the rows---
- public Cursor getPreviousUsers(String userName)
- {
- Cursor uCursor = null;
- System.out.print("\n\n\n\nUSERNAME VALUE: " + userName);
- Log.d("HELLO HELLO HELLO", userName);
- //db.execSQL("insert into " +DATABASE_TABLE_USERS+ "(" + KEY_ROW_ID+ "," +KEY_USERNAME+", "+KEY_PASSWORD+") values(3,'admin', 'admin')");
- try{
- uCursor = db.query(true, DATABASE_TABLE_USERS, new String[]
- {
- KEY_ROW_ID,
- KEY_USERNAME,
- KEY_PASSWORD
- },
- KEY_USERNAME + " LIKE '" + userName + "' ", null, null, null, null, null);
- //null, null, null, null, null);
- if (uCursor != null) {
- uCursor.moveToFirst();
- }
- else if (uCursor == null){
- System.out.print("TEST ME!!!!");
- }
- }catch (SQLException e){
- Log.e("error", "Failed to get all tasks: " + e);
- }catch (NullPointerException e){
- Log.e("Error", "null pointer error");
- }
- return uCursor;
- }
- //---retrieves all the rows---
- public Cursor getAllHeroes()
- {
- Cursor mCursor = null;
- try{
- mCursor = db.query(DATABASE_TABLE, new String[]
- {
- KEY_HERO_NAME,
- KEY_HERO_DESC,
- KEY_HERO_ABILITIES
- },
- null, null, null, null, null);
- }catch (SQLException e){
- Log.e("error", "Failed to get all tasks: " + e);
- }
- return mCursor;
- }
- public Cursor getOneHero(String heroName)
- {
- Cursor nCursor = null;
- try{
- nCursor = db.query(DATABASE_TABLE, new String[]
- {
- KEY_HERO_NAME,
- KEY_HERO_DESC,
- KEY_HERO_ABILITIES
- },
- KEY_HERO_NAME + " LIKE '" + heroName + "' ", null, null, null, null, null);
- //null, null, null, null, null);
- if (nCursor != null) {
- nCursor.moveToFirst();
- }
- else{
- Log.d("NOTE: " , "No result found");
- }
- }catch (SQLException e){
- Log.e("error", "Failed to get all tasks: " + e);
- }
- return nCursor;
- }
- //---updates a person---
- //public boolean updateTask(long rowId, String taskName, String taskDesc, int completeStatus)
- //{
- //ContentValues args = new ContentValues();
- // args.put(KEY_HERO_NAME, taskName);
- //args.put(KEY_HERO_DESC, taskDesc);
- //args.put(KEY_HERO_ABILITIES, completeStatus);
- //return db.update(DATABASE_TABLE, args, KEY_ROW_ID + "=" + rowId, null) > 0;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement