dbUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. package com.usai.util;
  2. import com.usai.apex.ApexTrackingApplication;
  3. import com.usai.apex.ResultActivity;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  9. import android.database.sqlite.SQLiteException;
  10. import android.net.Uri;
  11. import android.util.Log;
  12. public class dbUtil
  13. {
  14. private static final String[] FIELD_ID = { "_id" };
  15. // public static final String ZONECODE
  16. // public static final int FILTER_TYPE_MSG = 0;
  17. // public static final int FILTER_TYPE_BOTH = 1;
  18. // public static final int FILTER_TYPE_CALL = 2;
  19. // public static final int RECORD_TYPE_CALL = 2;
  20. // public static final int RECORD_TYPE_MSG = 1;
  21. //
  22. // public static final int NUMBER_TYPE_NULL = 0;
  23. // public static final int NUMBER_TYPE_NORMAL = 1;
  24. // public static final int NUMBER_TYPE_BLACK = 2;
  25. // public static final int NUMBER_TYPE_WHITE = 3;
  26. // public static final int NUMBER_TYPE_GRAY = 4;
  27. // public static final int NUMBER_TYPE_UNKNOWN = 7;
  28. // public static final int NUMBER_TYPE_CONTACT = 5;
  29. // public static final int NUMBER_TYPE_SUSPECT = 6;
  30. // public static final int FILTER_SOURCE_BLACKLIST = 0;
  31. // public static final int FILTER_SOURCE_SINGLERING = 1;
  32. // public static final int FILTER_SOURCE_KEYWORDS = 2;
  33. // public static final int FILTER_SOURCE_NONUMBER = 3;
  34. // public static void AddNumberToBlacklist(Context context, String Number) {
  35. // SQLiteDatabase db;
  36. // db = dbUtil.OpenDB(context, null, true);
  37. // boolean bexist = isRecordExist(db, "user_numberlist", "number = '"
  38. // + Number + "'");
  39. // if (bexist == false) {
  40. // ContentValues cv = new ContentValues();
  41. // cv.put("number", Number);
  42. // cv.put("type", dbUtil.NUMBER_TYPE_BLACK);
  43. // cv.put("burntype", 3);
  44. // cv.put("cimm", PhoneNumberUtils.toCallerIDMinMatch(Number));
  45. // db.insert("user_numberlist", null, cv);
  46. // }
  47. // dbUtil.CloseDB(db);
  48. // }
  49. // public static int KeywordsFilter(Context context, String Msg) {
  50. // int ret = -1;
  51. // boolean bKeywordsFilter = context.getSharedPreferences("PhoneAsstPref",
  52. // 0).getBoolean("KeywordsFilterPref", false);
  53. //
  54. // // = context.getSharedPreferences("PhoneAsstPref", 0)
  55. // // .getBoolean("BlockUnknownNumber", false);
  56. // if (bKeywordsFilter == false)
  57. // return ret;
  58. // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
  59. //
  60. // Cursor cursor = null;
  61. // try {
  62. //
  63. // cursor = db.query("user_keywords", new String[] { "_id", "words" },
  64. // null, null, null, null, "level desc", null);
  65. //
  66. // while (cursor.moveToNext()) {
  67. // String keywords = cursor.getString(1);
  68. // String[] spliter = keywords.split(" ");
  69. //
  70. // if (spliter.length == 0)
  71. // continue;
  72. //
  73. // int ifound = spliter.length; // 鍏抽敭瀛椾釜鏁� // for (int idx = 0; idx <
  74. // spliter.length; idx++) {
  75. // // 鍏煎浠ュ墠鐗堟湰鍏抽敭瀛楁湁澶氱┖鏍兼儏鍐碉紱
  76. // if (spliter[idx].equals("")) {
  77. // ifound--; // 鎺掗櫎璇绠楃殑绌烘暟閲� // continue;
  78. // }
  79. //
  80. // if (Msg.indexOf(spliter[idx]) != -1) {
  81. // ifound--;
  82. // }
  83. // }
  84. // if (ifound == 0) {
  85. // int id = cursor.getInt(0);
  86. // db.execSQL("update " + "user_keywords"
  87. // + " set level = level+1 where _id=" + id);
  88. // ret = id;
  89. // break;// break while
  90. // }
  91. //
  92. // }
  93. // } catch (SQLiteException e) {
  94. // dbgUtil.Log(Log.ERROR, "sql_err", e.toString());
  95. // } finally {
  96. // dbUtil.CloseCursor(cursor);
  97. // dbUtil.CloseDB(db);
  98. // }
  99. // return ret;
  100. // }
  101. // public static final int NUMBER_TYPE_UNKNOWN = 5;//no number ,hide number
  102. // ect;
  103. public static boolean isContentExist(Context context, Uri uri, String where)
  104. {
  105. boolean ret;
  106. Cursor cursor = context.getContentResolver().query(uri, null, where,
  107. null, null);
  108. dbgUtil.dbgCursorInfo(cursor, context);
  109. if (cursor.moveToNext())
  110. ret = true;
  111. else
  112. ret = false;
  113. dbUtil.CloseCursor(cursor);
  114. return ret;
  115. }
  116. // public static int query_NumberType(SQLiteDatabase db, String number) {
  117. // String cimm = PhoneNumberUtils.toCallerIDMinMatch(number);
  118. // int ret = -1;
  119. // Cursor cursor = db.query("user_numberlist", new String[] { "type",
  120. // "number" }, "cimm = '" + cimm + "'", null, null, null, null);
  121. // while (cursor.moveToNext()) {
  122. // if (PhoneNumberUtils.compare(number, cursor.getString(1))) {
  123. // dbgUtil.Log(
  124. // Log.ERROR,
  125. // "QUERY FROM user_numberlist",
  126. // number + "=" + cursor.getString(1) + "type="
  127. // + cursor.getInt(0));
  128. // ret = cursor.getInt(0);
  129. // break;
  130. // }
  131. // }
  132. // CloseCursor(cursor);
  133. // return ret;
  134. // }
  135. // static boolean isContact(Context context, String number) {
  136. // // String cimm=PhoneNumberUtils.toCallerIDMinMatch(number);
  137. // return dbUtil.isContentExist(context,
  138. // // ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  139. // Uri.withAppendedPath(
  140. // ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
  141. // Uri.encode(number)),
  142. // // ContactsContract.CommonDataKinds.Phone.NUMBER + "= '" +
  143. // // number+ "'");
  144. // /* "min_match = '" + cimm+ "'" */null);
  145. // }
  146. // static boolean isSuspect(SQLiteDatabase db, String number) {
  147. // return dbUtil.isRecordExist(db, "user_suspectlist", "number = '"
  148. // + number + "'");
  149. // }
  150. // public static int get_CallNumberType_old(Context context,
  151. // String incomingNumber)
  152. // {
  153. // int type = NUMBER_TYPE_NULL;
  154. // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
  155. //
  156. // boolean bBlockUnknown = context
  157. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  158. // "BlockUnknownNumber", false);
  159. // boolean bignorcontact = context
  160. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  161. // "IgnoreContact", false);
  162. // if ((incomingNumber == null || incomingNumber.equals("")))
  163. // if (bBlockUnknown)
  164. // type = NUMBER_TYPE_BLACK;
  165. // else
  166. // type = NUMBER_TYPE_NORMAL;
  167. // else
  168. // {
  169. // type = query_NumberType(db, incomingNumber);
  170. // if (type == NUMBER_TYPE_WHITE)
  171. // {
  172. //
  173. // }
  174. // else
  175. // if (bignorcontact && isContact(context, incomingNumber))
  176. // {
  177. // type = NUMBER_TYPE_CONTACT;
  178. // dbgUtil.Log(Log.ERROR,null, "number type contact");
  179. // }
  180. // else
  181. // if (type == NUMBER_TYPE_BLACK)
  182. // {
  183. // }
  184. // else
  185. // if (dbUtil.isRecordExist(db, "user_suspectlist",
  186. // "number = '" + incomingNumber + "'"))
  187. // {
  188. // type = NUMBER_TYPE_SUSPECT;
  189. // }
  190. // else
  191. // {
  192. // type = NUMBER_TYPE_NORMAL;
  193. //
  194. // }
  195. // }
  196. // dbUtil.CloseDB(db);
  197. //
  198. // dbgUtil.Log(Log.ERROR,"get_CallNumberType",
  199. // "number="+incomingNumber+" type="+type);
  200. // return type;
  201. //
  202. // }
  203. // public static NumberInfo get_NumberInfo(Context context,
  204. // String incomingNumber)
  205. // {
  206. // NumberInfo numberInfo = new NumberInfo();
  207. // numberInfo.incomingNumber = incomingNumber;
  208. // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
  209. //
  210. // // boolean bBlockUnknown = context
  211. // // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  212. // // "BlockUnknownNumber", false);
  213. // // boolean bignorcontact = context
  214. // // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  215. // // "IgnoreContact", false);
  216. //
  217. // // int type = NUMBER_TYPE_NULL;
  218. // if (incomingNumber == null || incomingNumber.length() < 3)
  219. // {
  220. // numberInfo.BaseType = NUMBER_TYPE_UNKNOWN;
  221. // }
  222. // else
  223. // {
  224. // numberInfo.BaseType = query_NumberType(db, incomingNumber);
  225. // if (numberInfo.BaseType == -1)// not in user_numberlist;
  226. // {
  227. // numberInfo.BaseType = NUMBER_TYPE_NORMAL;
  228. //
  229. // // else
  230. // // if (isSuspect(db, incomingNumber))
  231. // // {
  232. // // type = NUMBER_TYPE_SUSPECT;
  233. // //
  234. // // }
  235. // // else
  236. // // {
  237. // // type = NUMBER_TYPE_NORMAL;
  238. // // }
  239. // }
  240. // if (isContact(context, incomingNumber))
  241. // {
  242. // numberInfo.isContact = true;
  243. // // type = NUMBER_TYPE_CONTACT;
  244. // }
  245. // if (isSuspect(db, incomingNumber))
  246. // {
  247. // numberInfo.isSuspect = true;
  248. //
  249. // }
  250. //
  251. // // else
  252. // // if (type == NUMBER_TYPE_BLACK)
  253. // // {
  254. // // if (isContact(context, incomingNumber))
  255. // // {
  256. // // type = NUMBER_TYPE_CONTACT;
  257. // // }
  258. // // }
  259. // }
  260. // dbUtil.CloseDB(db);
  261. //
  262. // dbgUtil.Log(Log.ERROR, "get_NumberInfo", "number=" + incomingNumber
  263. // + " type=" + numberInfo.BaseType);
  264. // return numberInfo;
  265. //
  266. // }
  267. // public static int get_SmsNumberType(Context context, String
  268. // incomingNumber)
  269. // {
  270. // SQLiteDatabase db = dbUtil.OpenDB(context, null, false);
  271. //
  272. // boolean bBlockUnknown = context
  273. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  274. // "BlockUnknownNumber", false);
  275. // boolean bignorcontact = context
  276. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  277. // "IgnoreContact", false);
  278. //
  279. // int type = NUMBER_TYPE_NULL;
  280. // if ((incomingNumber == null || incomingNumber.equals("")))
  281. // if (bBlockUnknown)
  282. // type = NUMBER_TYPE_BLACK;
  283. // else
  284. // type = NUMBER_TYPE_NORMAL;
  285. // else
  286. // {
  287. // type = query_NumberType(db, incomingNumber);
  288. // if (type == NUMBER_TYPE_WHITE)
  289. // {
  290. //
  291. // }
  292. // else
  293. // if (bignorcontact && isContact(context, incomingNumber))
  294. // {
  295. // type = NUMBER_TYPE_CONTACT;
  296. // dbgUtil.Log(Log.ERROR, null, "number type contact");
  297. // }
  298. // else
  299. // if (type == NUMBER_TYPE_BLACK)
  300. // {
  301. // }
  302. // else
  303. // {
  304. // type = NUMBER_TYPE_NORMAL;
  305. //
  306. // }
  307. // }
  308. // dbUtil.CloseDB(db);
  309. //
  310. // return type;
  311. //
  312. // }
  313. public static void CloseDB(SQLiteDatabase db)
  314. {
  315. if (db == null)
  316. return;
  317. if (db.isOpen() == false)
  318. return;
  319. db.close();
  320. return;
  321. }
  322. public static void CloseCursor(Cursor c)
  323. {
  324. if (c == null)
  325. return;
  326. if (c.isClosed())
  327. return;
  328. c.close();
  329. return;
  330. }
  331. public static SQLiteDatabase OpenDB(Context context, CursorFactory factory,
  332. boolean writable)
  333. {
  334. SqlOpenHelper dbHelper = new SqlOpenHelper(context, "apex.db", factory,
  335. 1);
  336. dbHelper.addInitSQL("create table users (" + "_id INTEGER PRIMARY KEY,"
  337. + "name varchar(20)," + "pass varchar(20))");
  338. // dbHelper.addInitSQL("create table scan_records ("
  339. // + "_id INTEGER PRIMARY KEY," + "submit BOOLEAN DEFAULT (0),"
  340. // + "picker VARCHAR(20)," + "pid varchar(20))");
  341. /*
  342. *
  343. * name : field name aname: alias name for display field_type:
  344. * int,bool,varchar... function_name: Ocean Booking , Ocean B/L Info.,
  345. * ... behavior: search / result priority: display order show: whether
  346. * show this field abandon: true/false , uses for update auth_info user:
  347. * record owner
  348. */
  349. dbHelper.addInitSQL("CREATE TABLE fields_info ("
  350. + "_id INTEGER PRIMARY KEY," + "name VARCHAR(20),"
  351. + "aname VARCHAR(20)," + "field_type INTEGER,"
  352. + "function_name VARCHAR(20)," + "behavior INTEGER,"
  353. + "priority INTEGER," + "show BOOLEAN," + "abandon BOOLEAN,"
  354. + "user VARCHAR(20))");
  355. dbHelper.addInitSQL("CREATE TABLE actions_info ("
  356. + "_id INTEGER PRIMARY KEY," + "name VARCHAR(20),"
  357. + "function_name VARCHAR(20)," + "priority INTEGER,"
  358. + "abandon BOOLEAN," + "user VARCHAR(20))");
  359. dbHelper.addInitSQL("CREATE TABLE search_history ("
  360. + "_id INTEGER PRIMARY KEY," + "h_val VARCHAR(20),"
  361. + "h_field VARCHAR(20)," + "h_time timestamp,"
  362. + "user VARCHAR(20))");
  363. // dbHelper.addInitSQL("create table pics (" +
  364. // "_id INTEGER PRIMARY KEY,"
  365. // + "picker varchar(20)," + "err_code INTEGER ,"
  366. // + "pid varchar(20)," + "local_path varchar(256))");
  367. SQLiteDatabase db = dbHelper.OpenDB(writable);
  368. return db;
  369. }
  370. public static void removeRecords(SQLiteDatabase db, String table,
  371. String whereclause)
  372. {
  373. db.execSQL("delete from " + table + " where " + whereclause);
  374. }
  375. public static long addRecordsFromCursor(SQLiteDatabase db,
  376. String tablename, Cursor source, String[] from, String[] to,
  377. boolean dumpPrimaryKey, boolean dumpOneRecord)
  378. {
  379. int ret = -1;
  380. ContentValues cv = new ContentValues();
  381. if (from == null)
  382. from = source.getColumnNames();
  383. if (to == null)
  384. to = source.getColumnNames();
  385. if (from.length != to.length)// 瀛楁鏁颁笉鍖归厤
  386. return ret;
  387. while (source.moveToNext())
  388. {
  389. for (int i = 0; i < from.length; i++)
  390. {
  391. if (from[i].equals("_id"))
  392. {
  393. if (dumpPrimaryKey)
  394. cv.put(to[i], source.getInt(i));
  395. }
  396. else
  397. {
  398. cv.put(to[i], source.getString(i));
  399. }
  400. }
  401. db.insert(tablename, null, cv);
  402. if (dumpOneRecord)
  403. break;
  404. }
  405. ret = 1;
  406. return ret;
  407. }
  408. // public static long addRecord(SQLiteDatabase db, String table,
  409. // ContentValues record)
  410. // {
  411. // long ret = -1;
  412. // try
  413. // {
  414. // ret = db.insert(table, null, record);
  415. // }
  416. // catch (SQLiteException e)
  417. // {
  418. // dbgUtil.Log(Log.ERROR,"sql_err", e.toString());
  419. //
  420. // }
  421. // return ret;
  422. // }
  423. public static boolean isTableEmpty(SQLiteDatabase db, String table)
  424. {
  425. boolean ret = true;
  426. Cursor cursor = null;
  427. try
  428. {
  429. cursor = db.query(table, FIELD_ID, null, null, null, null, null,
  430. "1");
  431. if (cursor.moveToNext())
  432. {
  433. // cursor.close();
  434. ret = false;
  435. }
  436. else
  437. ret = true;
  438. }
  439. catch (SQLiteException e)
  440. {
  441. Log.e("sql_err", e.toString());
  442. }
  443. finally
  444. {
  445. dbUtil.CloseCursor(cursor);
  446. }
  447. return ret;
  448. }
  449. public static boolean isRecordExist(SQLiteDatabase db, String table,
  450. String where)
  451. {
  452. boolean ret = false;
  453. Cursor cursor = null;
  454. try
  455. {
  456. cursor = db.query(table, FIELD_ID, where, null, null, null, null,
  457. null);
  458. if (cursor.moveToNext())
  459. ret = true;
  460. else
  461. ret = false;
  462. }
  463. catch (SQLiteException e)
  464. {
  465. Log.e("sql_err", e.toString());
  466. }
  467. finally
  468. {
  469. dbUtil.CloseCursor(cursor);
  470. }
  471. return ret;
  472. }
  473. public static String get_fields(String user, String module)
  474. {
  475. SQLiteDatabase db = OpenDB(ApexTrackingApplication.get_instance()
  476. .getApplicationContext(), null, false);
  477. Cursor cursor = db.query("fields_info", new String[] { "name" },
  478. "function_name='" + module + "' and user='" + user
  479. + "' and behavior=" + Network.BEHAVIOR_RESULT, null,
  480. null, null, null, null);
  481. String ret = "";
  482. while (cursor.moveToNext())
  483. {
  484. ret += cursor.getString(0);
  485. ret += ",";
  486. }
  487. ret = ret.substring(0, ret.length() - 1);
  488. CloseCursor(cursor);
  489. CloseDB(db);
  490. return ret;
  491. }
  492. public static int get_count(SQLiteDatabase db,String table,String where)
  493. {
  494. int ret=0;
  495. Cursor cursor = db.query(table,
  496. new String[] { "count(*)" }, where,
  497. null, null, null, null, null);
  498. if (cursor.moveToNext()) {
  499. ret = cursor.getInt(0);
  500. }
  501. dbUtil.CloseCursor(cursor);
  502. return ret;
  503. }
  504. public static int get_recordid(SQLiteDatabase db, String table, String where )
  505. {
  506. return get_recordid(db,table,where,null);
  507. }
  508. public static int get_recordid(SQLiteDatabase db, String table, String where ,String Orderby)
  509. {
  510. int ret = -1;
  511. Cursor cursor = null;
  512. try
  513. {
  514. cursor = db.query(table, FIELD_ID, where, null, null, null, Orderby,
  515. null);
  516. if (cursor.moveToNext())
  517. ret = cursor.getInt(0);
  518. else
  519. ret = -1;
  520. }
  521. catch (SQLiteException e)
  522. {
  523. Log.e("sql_err", e.toString());
  524. }
  525. finally
  526. {
  527. dbUtil.CloseCursor(cursor);
  528. }
  529. return ret;
  530. }
  531. // if(isTableEmpty(db,"block_records"))
  532. }