Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. package com.example.alannah.overwatch;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. public class MyDBManager
  13. {
  14. final String TAG = MyDBManager.class.getSimpleName();
  15. // These are the names of the columns the table will contain
  16. public static final String KEY_ROW_ID = "_id";
  17. public static final String KEY_HERO_NAME = "hero_name";
  18. public static final String KEY_HERO_DESC = "hero_desc";
  19. public static final String KEY_HERO_ABILITIES = "hero_abilities";
  20. private static final String DATABASE_NAME = "Heroes";
  21. private static final String DATABASE_TABLE = "Descriptions";
  22. private static final int DATABASE_VERSION = 2;
  23. // This is the string containing the SQL database create statement
  24. private static final String DATABASE_CREATE =
  25. "create table " + DATABASE_TABLE +
  26. " (" + KEY_ROW_ID + " integer primary key autoincrement, " +
  27. KEY_HERO_NAME + " text not null, " +
  28. KEY_HERO_DESC + " text not null, " +
  29. KEY_HERO_ABILITIES + " integer not null);";
  30.  
  31.  
  32. public static final String KEY_USERNAME = "username";
  33. public static final String KEY_PASSWORD = "password";
  34. private static final String DATABASE_TABLE_USERS = " users";
  35. private static final String DATABASE_CREATE_USERS =
  36. "create table " + DATABASE_TABLE_USERS +
  37. " (" + KEY_ROW_ID + " integer primary key autoincrement, "
  38. +KEY_USERNAME + " text not null, " +
  39. KEY_PASSWORD + " text not null);";
  40.  
  41. private final Context context;
  42. private DatabaseHelper DBHelper;
  43. private SQLiteDatabase db;
  44.  
  45. // constructor for your class
  46. public MyDBManager(Context ctx) {
  47. // Context is a way that Android transfers info about Activities and apps.
  48. this.context = ctx;
  49. DBHelper = new DatabaseHelper(context);
  50. }
  51.  
  52. private static class DatabaseHelper extends SQLiteOpenHelper {
  53. // constructor for your dB helper class. This code is standard. You’ve set up the parameter values for the constructor already...database name,etc
  54. DatabaseHelper(Context context) {
  55. // This is the helper class that will create the dB if it doesn’t exist and upgrades it if
  56. // the structure has changed. It needs a constructor, an onCreate() method and an onUpgrade() method
  57. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  58. }
  59. // end of the help class
  60.  
  61.  
  62. @Override
  63. public void onCreate(SQLiteDatabase db) {
  64. // The “Database_create” string below needs to contain the SQL statement needed to create the dB
  65. // db.delete(DATABASE_TABLE,null,null);
  66. db.execSQL(DATABASE_CREATE);
  67. db.execSQL(DATABASE_CREATE_USERS);
  68. db.execSQL("insert into" +DATABASE_TABLE_USERS+ "("+KEY_USERNAME+", "+KEY_PASSWORD+") values('[admin]', '[admin]')");
  69. db.execSQL("insert into" +DATABASE_TABLE_USERS+ "("+KEY_USERNAME+", "+KEY_PASSWORD+") values('[admin2]', '[admin2]')");
  70. }
  71.  
  72. @Override
  73. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  74. // db.delete(DATABASE_TABLE,null,null);
  75. db.execSQL(DATABASE_CREATE);
  76. db.execSQL(DATABASE_CREATE_USERS);
  77. }
  78. }
  79.  
  80.  
  81.  
  82. // If you want to change the structure of your database, e.g. Add a new column to a table,
  83. // the code will go head..
  84. //This method only triggers if the database version number has increased
  85. // from here on, include whatever methods will be used to access or change data in the database
  86. //---opens the database--- any activity that uses the dB will need to do this
  87.  
  88.  
  89. public MyDBManager open()
  90. {
  91.  
  92. try{
  93. db = DBHelper.getWritableDatabase();
  94. }catch(SQLException e) {
  95. Log.e("error", "Failed to open database: " + e);
  96. }
  97.  
  98. return this;
  99. }
  100.  
  101. //---closes the database--- any activity that uses the dB will need to do this
  102. public void close()
  103. {
  104. DBHelper.close();
  105. }
  106.  
  107. //---insert a hero into the database---
  108. public long insertHero(String heroName, String heroDesc, String heroAbilities) {
  109.  
  110. ContentValues initialValues = new ContentValues();
  111. initialValues.put(KEY_HERO_NAME, heroName);
  112. initialValues.put(KEY_HERO_DESC, heroDesc);
  113. initialValues.put(KEY_HERO_ABILITIES, heroAbilities);
  114. return db.insert(DATABASE_TABLE, null, initialValues);
  115. }
  116. //---insert a user into the database---
  117. public long insertUser(String username, String password) {
  118.  
  119. ContentValues initialValues = new ContentValues();
  120. initialValues.put(KEY_USERNAME, username);
  121. initialValues.put(KEY_PASSWORD, password);
  122. return db.insert(DATABASE_TABLE_USERS, null, initialValues);
  123. }
  124. //---retrieves all the rows---
  125. public Cursor getPreviousUsers(String userName)
  126. {
  127. Cursor uCursor = null;
  128. System.out.print("\n\n\n\nUSERNAME VALUE: " + userName);
  129. Log.d("HELLO HELLO HELLO", userName);
  130.  
  131. //db.execSQL("insert into " +DATABASE_TABLE_USERS+ "(" + KEY_ROW_ID+ "," +KEY_USERNAME+", "+KEY_PASSWORD+") values(3,'admin', 'admin')");
  132.  
  133. try{
  134. uCursor = db.query(true, DATABASE_TABLE_USERS, new String[]
  135. {
  136. KEY_ROW_ID,
  137. KEY_USERNAME,
  138. KEY_PASSWORD
  139.  
  140. },
  141. KEY_USERNAME + " LIKE '" + userName + "' ", null, null, null, null, null);
  142. //null, null, null, null, null);
  143. if (uCursor != null) {
  144. uCursor.moveToFirst();
  145. }
  146. else if (uCursor == null){
  147. System.out.print("TEST ME!!!!");
  148. }
  149.  
  150. }catch (SQLException e){
  151. Log.e("error", "Failed to get all tasks: " + e);
  152.  
  153. }catch (NullPointerException e){
  154. Log.e("Error", "null pointer error");
  155.  
  156. }
  157.  
  158. return uCursor;
  159.  
  160. }
  161. //---retrieves all the rows---
  162. public Cursor getAllHeroes()
  163. {
  164. Cursor mCursor = null;
  165.  
  166. try{
  167. mCursor = db.query(DATABASE_TABLE, new String[]
  168. {
  169. KEY_HERO_NAME,
  170. KEY_HERO_DESC,
  171. KEY_HERO_ABILITIES
  172.  
  173. },
  174. null, null, null, null, null);
  175.  
  176. }catch (SQLException e){
  177. Log.e("error", "Failed to get all tasks: " + e);
  178. }
  179.  
  180. return mCursor;
  181. }
  182.  
  183. public Cursor getOneHero(String heroName)
  184. {
  185. Cursor nCursor = null;
  186.  
  187. try{
  188. nCursor = db.query(DATABASE_TABLE, new String[]
  189. {
  190.  
  191. KEY_HERO_NAME,
  192. KEY_HERO_DESC,
  193. KEY_HERO_ABILITIES
  194.  
  195. },
  196. KEY_HERO_NAME + " LIKE '" + heroName + "' ", null, null, null, null, null);
  197. //null, null, null, null, null);
  198. if (nCursor != null) {
  199. nCursor.moveToFirst();
  200. }
  201. else{
  202. Log.d("NOTE: " , "No result found");
  203.  
  204. }
  205.  
  206. }catch (SQLException e){
  207. Log.e("error", "Failed to get all tasks: " + e);
  208. }
  209.  
  210. return nCursor;
  211. }
  212.  
  213.  
  214. //---updates a person---
  215. //public boolean updateTask(long rowId, String taskName, String taskDesc, int completeStatus)
  216. //{
  217. //ContentValues args = new ContentValues();
  218. // args.put(KEY_HERO_NAME, taskName);
  219. //args.put(KEY_HERO_DESC, taskDesc);
  220. //args.put(KEY_HERO_ABILITIES, completeStatus);
  221. //return db.update(DATABASE_TABLE, args, KEY_ROW_ID + "=" + rowId, null) > 0;
  222. // }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement