Guest User

Untitled

a guest
Aug 1st, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. package com.example.xyz;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.StringTokenizer;
  10.  
  11. import android.content.ContentValues;
  12. import android.content.Context;
  13. import android.database.Cursor;
  14. import android.database.sqlite.SQLiteDatabase;
  15. import android.database.sqlite.SQLiteOpenHelper;
  16. import android.util.Log;
  17.  
  18. public class DatabaseHandler extends SQLiteOpenHelper {
  19.  
  20. // All Static variables
  21. // Database Version
  22. private static final int DATABASE_VERSION = 2;
  23.  
  24. // Database Name
  25. private static final String DATABASE_NAME = "feedsmanager";
  26.  
  27. // Contacts table name
  28. private static final String TABLE_CONTACTS = "table_to_hold_all_values";
  29.  
  30. // Contacts Table Columns names
  31. private static final String KEY_ID = "id";
  32. private static final String KEY_NAME = "name";//ctid
  33. private static final String KEY_PH_NO = "phone_number";//feedname,address;feedname;address etc
  34. Context ctx;
  35.  
  36. public DatabaseHandler(Context context) {
  37. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  38. ctx=context;
  39. }
  40.  
  41. // Creating Tables
  42. @Override
  43. public void onCreate(SQLiteDatabase db) {
  44.  
  45. //Log.d("in onCreate","twitch1");
  46. String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
  47. + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
  48. + KEY_PH_NO + " TEXT);";
  49. db.execSQL(CREATE_CONTACTS_TABLE);
  50. String line="";
  51. InputStream is=null;
  52.  
  53. try{
  54.  
  55. File fileq = ctx.getFileStreamPath("feedtitlesandaddresses.txt");
  56. if(!fileq.exists()){
  57. is = ctx.getAssets().open("feedtitlesandaddresses.txt");
  58. //Log.d("indatabasefileexists false","fubar");
  59. }else{
  60. is = ctx.openFileInput("feedtitlesandaddresses.txt");
  61. //Log.d("indatabasefileexists true","fubar");
  62. }
  63.  
  64. InputStreamReader iz=new InputStreamReader(is);
  65. BufferedReader br = new BufferedReader(iz);//Log.d("fcked here8","twitch1");
  66. ContentValues values = new ContentValues();
  67. try{db.beginTransaction();
  68. while((line=br.readLine())!=null) {
  69. //Log.d(line,"twitch11");
  70. //Log.d("fcked here6","twitch1");
  71. StringTokenizer stringTokenizer = new StringTokenizer(line, "<");
  72. //ctid
  73. String firstNumber="";
  74. String strfinal="";//feedname,address;feedname;address etc
  75. firstNumber = (String) stringTokenizer.nextElement();
  76. char c = '<'; // search for this character ...
  77. int count = 0;
  78. int size=0;
  79. for (int i = 0 ; i < line.length() ; i++ ) {
  80. if ( line.charAt(i) == c ) {
  81. count += 1;
  82. }}size=count/6;
  83.  
  84. String str="";
  85. String str1="";
  86. String st[]= line.split("title>");
  87.  
  88. int t=1;
  89. for(int q=1;q<=(size);q++){
  90.  
  91. if(q!=(size)){
  92. str=st[t].substring(0,st[t].length()-2)+";";
  93. t+=1;
  94. str1=st[t];
  95. t+=1;
  96. str=str+str1.substring(10,str1.length()-27)+">";
  97. strfinal+=str;
  98. //Log.d(strfinal,"twitch11");
  99. }
  100. else if(q==(size))
  101. {
  102. str=st[t].substring(0,st[t].length()-2)+";";
  103. t+=1;
  104. str1=st[t];
  105. t+=1;//Log.d("This is sparta","hitlers");
  106. str=str+str1.substring(10,str1.length()-20)+">";
  107. strfinal+=str;
  108. }//Log.d(strfinal,"twitch1");
  109.  
  110. }
  111. //Log.d("cked here1","twitch1");
  112. values.put(KEY_NAME, firstNumber); // Contact Name
  113. values.put(KEY_PH_NO,strfinal); // Contact Phone
  114. //Log.d("cked here2","twitch1");
  115. // Inserting Row
  116. //Log.d("Inserting","YOYO");
  117. db.insert(TABLE_CONTACTS, null, values);}
  118. db.setTransactionSuccessful();
  119. }finally {
  120.  
  121. db.endTransaction();
  122.  
  123. }
  124. //Log.d("cked here4","twitch1");
  125. }catch(Exception e){Log.d("yeah error is"+e,"twitch11");}
  126.  
  127. }
  128. //populating database on create
  129. void populate(SQLiteDatabase dbs){
  130. try{
  131. InputStream is = ctx.getAssets().open("feedtitlesandaddresses.txt");
  132. InputStreamReader iz=new InputStreamReader(is);
  133. BufferedReader br = new BufferedReader(iz);//Log.d("fcked here8","twitch1");
  134. ContentValues values = new ContentValues();
  135.  
  136. values.put(KEY_NAME, "NAME"); // Contact Name
  137. values.put(KEY_PH_NO, "BLA"); // Contact Phone
  138. dbs.insert(TABLE_CONTACTS, null, values);
  139. dbs.close();}catch(Exception e){Log.d("yeah error is"+e,"twitch12");}
  140. }
  141. // Upgrading database
  142. @Override
  143. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  144. // Drop older table if existed
  145.  
  146. // Log.d("in onupgrade","twitch");
  147. db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
  148.  
  149. // Create tables again
  150. onCreate(db);
  151. }
  152.  
  153. /**
  154. * All CRUD(Create, Read, Update, Delete) Operations
  155. */
  156.  
  157. // Adding new contact
  158. void addContact(Contact contact) {
  159. SQLiteDatabase db = this.getWritableDatabase();
  160.  
  161. ContentValues values = new ContentValues();
  162. values.put(KEY_NAME, contact.getName()); // Contact Name
  163. values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
  164.  
  165. // Inserting Row
  166. db.insert(TABLE_CONTACTS, null, values);
  167. db.close(); // Closing database connection
  168. }
  169.  
  170. // Getting single contact
  171. Contact getContact(int id) {
  172. SQLiteDatabase db = this.getReadableDatabase();
  173.  
  174. Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
  175. KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
  176. new String[] { String.valueOf(id) }, null, null, null, null);
  177. if (cursor != null)
  178. cursor.moveToFirst();
  179.  
  180. Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
  181. cursor.getString(1), cursor.getString(2));
  182. // return contact
  183. return contact;
  184. }
  185.  
  186. // Getting All Contacts
  187. public List<Contact> getAllContacts() {
  188. List<Contact> contactList = new ArrayList<Contact>();
  189. // Select All Query
  190. String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
  191.  
  192. SQLiteDatabase db = this.getWritableDatabase();
  193. Cursor cursor = db.rawQuery(selectQuery, null);
  194.  
  195. // looping through all rows and adding to list
  196. if (cursor.moveToFirst()) {
  197. do {
  198. Contact contact = new Contact();
  199. contact.setID(Integer.parseInt(cursor.getString(0)));
  200. contact.setName(cursor.getString(1));
  201. contact.setPhoneNumber(cursor.getString(2));
  202. // Adding contact to list
  203. contactList.add(contact);
  204. } while (cursor.moveToNext());
  205. }
  206.  
  207. // return contact list
  208. return contactList;
  209. }
  210.  
  211. // Updating single contact
  212. public int updateContact(Contact contact) {
  213. SQLiteDatabase db = this.getWritableDatabase();
  214.  
  215. ContentValues values = new ContentValues();
  216. values.put(KEY_NAME, contact.getName());
  217. values.put(KEY_PH_NO, contact.getPhoneNumber());
  218.  
  219. // updating row
  220. return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
  221. new String[] { String.valueOf(contact.getID()) });
  222. }
  223.  
  224. // Deleting single contact
  225. public void deleteContact(Contact contact) {
  226. SQLiteDatabase db = this.getWritableDatabase();
  227. db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
  228. new String[] { String.valueOf(contact.getID()) });
  229. db.close();
  230. }
  231.  
  232. // Getting contacts Count
  233. public int getContactsCount() {
  234. String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
  235. SQLiteDatabase db = this.getReadableDatabase();
  236. Cursor cursor = db.rawQuery(countQuery, null);
  237. cursor.close();
  238. return cursor.getCount();
  239. }
  240.  
  241. }
Add Comment
Please, Sign In to add comment