dbUtil.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.usai.util;
  2. import com.usai.apex.ApexTrackingApplication;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  8. import android.database.sqlite.SQLiteException;
  9. import android.net.Uri;
  10. import android.util.Log;
  11. public class dbUtil
  12. {
  13. private static final String[] FIELD_ID = { "_id" };
  14. public static boolean isContentExist(Context context, Uri uri, String where)
  15. {
  16. boolean ret;
  17. Cursor cursor = context.getContentResolver().query(uri, null, where,
  18. null, null);
  19. dbgUtil.dbgCursorInfo(cursor, context);
  20. if (cursor.moveToNext())
  21. ret = true;
  22. else
  23. ret = false;
  24. dbUtil.CloseCursor(cursor);
  25. return ret;
  26. }
  27. public static void CloseDB(SQLiteDatabase db)
  28. {
  29. if (db == null)
  30. return;
  31. if (db.isOpen() == false)
  32. return;
  33. db.close();
  34. return;
  35. }
  36. public static void CloseCursor(Cursor c)
  37. {
  38. if (c == null)
  39. return;
  40. if (c.isClosed())
  41. return;
  42. c.close();
  43. return;
  44. }
  45. public static SQLiteDatabase OpenDB(Context context, CursorFactory factory,
  46. boolean writable)
  47. {
  48. SqlOpenHelper dbHelper = new SqlOpenHelper(context, "apex.db", factory,
  49. 2);
  50. dbHelper.addInitSQL("create table users (" + "_id INTEGER PRIMARY KEY,"
  51. + "name varchar(20)," + "pass varchar(20))");
  52. // dbHelper.addInitSQL("create table scan_records ("
  53. // + "_id INTEGER PRIMARY KEY," + "submit BOOLEAN DEFAULT (0),"
  54. // + "picker VARCHAR(20)," + "pid varchar(20))");
  55. /*
  56. *
  57. * name : field name aname: alias name for display field_type:
  58. * int,bool,varchar... function_name: Ocean Booking , Ocean B/L Info.,
  59. * ... behavior: search / result priority: display order show: whether
  60. * show this field abandon: true/false , uses for update auth_info user:
  61. * record owner
  62. */
  63. dbHelper.addInitSQL("CREATE TABLE fields_info ("
  64. + "_id INTEGER PRIMARY KEY," + "name VARCHAR(20),"
  65. + "aname VARCHAR(20)," + "field_type INTEGER,"
  66. + "function_name VARCHAR(20)," + "behavior INTEGER,"
  67. + "priority INTEGER," + "show BOOLEAN," + "abandon BOOLEAN,"
  68. + "user VARCHAR(20))");
  69. dbHelper.addInitSQL("CREATE TABLE actions_info ("
  70. + "_id INTEGER PRIMARY KEY," + "name VARCHAR(20),"
  71. + "function_name VARCHAR(20)," + "priority INTEGER,"
  72. + "abandon BOOLEAN," + "user VARCHAR(20))");
  73. dbHelper.addInitSQL("CREATE TABLE search_history ("
  74. + "_id INTEGER PRIMARY KEY," + "h_val VARCHAR(20),"
  75. + "h_field VARCHAR(20)," + "h_time timestamp,"
  76. + "user VARCHAR(20))");
  77. // new table must create in onupgrade function too;
  78. dbHelper.addInitSQL("CREATE TABLE push_message ("
  79. + "_id INTEGER PRIMARY KEY," + "s_id VARCHAR(20),"
  80. + "e_id VARCHAR(20)," + "msgcount INTEGER,"
  81. + "message VARCHAR(20)," + "h_time timestamp,"
  82. + "create_time timestamp," + "user VARCHAR(20),"
  83. + "read BOOLEAN)");
  84. dbHelper.addInitSQL("CREATE TABLE favorites ("
  85. + "_id INTEGER PRIMARY KEY," + "name VARCHAR(20),"
  86. + "params VARCHAR(20)," + "action VARCHAR(20),"
  87. + "module_name VARCHAR(20)," + "detail_id VARCHAR(20),"
  88. + "action_type VARCHAR(20)," + "create_time timestamp,"
  89. + "user VARCHAR(20))");
  90. dbHelper.addInitSQL("CREATE TABLE history ("
  91. + "_id INTEGER PRIMARY KEY," + "name VARCHAR(20),"
  92. + "params VARCHAR(20)," + "action VARCHAR(20),"
  93. + "module_name VARCHAR(20)," + "criterion VARCHAR(20),"
  94. + "create_time timestamp," + "user VARCHAR(20))");
  95. // dbHelper.addInitSQL("create table pics (" +
  96. // "_id INTEGER PRIMARY KEY,"
  97. // + "picker varchar(20)," + "err_code INTEGER ,"
  98. // + "pid varchar(20)," + "local_path varchar(256))");
  99. SQLiteDatabase db = dbHelper.OpenDB(writable);
  100. return db;
  101. }
  102. public static void removeRecords(SQLiteDatabase db, String table,
  103. String whereclause)
  104. {
  105. db.execSQL("delete from " + table + " where " + whereclause);
  106. }
  107. public static long addRecordsFromCursor(SQLiteDatabase db,
  108. String tablename, Cursor source, String[] from, String[] to,
  109. boolean dumpPrimaryKey, boolean dumpOneRecord)
  110. {
  111. int ret = -1;
  112. ContentValues cv = new ContentValues();
  113. if (from == null)
  114. from = source.getColumnNames();
  115. if (to == null)
  116. to = source.getColumnNames();
  117. if (from.length != to.length)
  118. return ret;
  119. while (source.moveToNext())
  120. {
  121. for (int i = 0; i < from.length; i++)
  122. {
  123. if (from[i].equals("_id"))
  124. {
  125. if (dumpPrimaryKey)
  126. cv.put(to[i], source.getInt(i));
  127. }
  128. else
  129. {
  130. cv.put(to[i], source.getString(i));
  131. }
  132. }
  133. db.insert(tablename, null, cv);
  134. if (dumpOneRecord)
  135. break;
  136. }
  137. ret = 1;
  138. return ret;
  139. }
  140. // public static long addRecord(SQLiteDatabase db, String table,
  141. // ContentValues record)
  142. // {
  143. // long ret = -1;
  144. // try
  145. // {
  146. // ret = db.insert(table, null, record);
  147. // }
  148. // catch (SQLiteException e)
  149. // {
  150. // dbgUtil.Log(Log.ERROR,"sql_err", e.toString());
  151. //
  152. // }
  153. // return ret;
  154. // }
  155. public static boolean isTableEmpty(SQLiteDatabase db, String table)
  156. {
  157. boolean ret = true;
  158. Cursor cursor = null;
  159. try
  160. {
  161. cursor = db.query(table, FIELD_ID, null, null, null, null, null,
  162. "1");
  163. if (cursor.moveToNext())
  164. {
  165. // cursor.close();
  166. ret = false;
  167. }
  168. else
  169. ret = true;
  170. }
  171. catch (SQLiteException e)
  172. {
  173. Log.e("sql_err", e.toString());
  174. }
  175. finally
  176. {
  177. dbUtil.CloseCursor(cursor);
  178. }
  179. return ret;
  180. }
  181. public static boolean isRecordExist(SQLiteDatabase db, String table,
  182. String where)
  183. {
  184. boolean ret = false;
  185. Cursor cursor = null;
  186. try
  187. {
  188. cursor = db.query(table, FIELD_ID, where, null, null, null, null,
  189. null);
  190. if (cursor.moveToNext())
  191. ret = true;
  192. else
  193. ret = false;
  194. }
  195. catch (SQLiteException e)
  196. {
  197. Log.e("sql_err", e.toString());
  198. }
  199. finally
  200. {
  201. dbUtil.CloseCursor(cursor);
  202. }
  203. return ret;
  204. }
  205. public static String get_fields(String user, String module)
  206. {
  207. SQLiteDatabase db = OpenDB(ApexTrackingApplication.get_instance()
  208. .getApplicationContext(), null, false);
  209. Cursor cursor = db.query("fields_info", new String[] { "name" },
  210. "function_name='" + module + "' and user='" + user
  211. + "' and behavior=" + Network.BEHAVIOR_RESULT, null,
  212. null, null, null, null);
  213. String ret = "";
  214. while (cursor.moveToNext())
  215. {
  216. ret += cursor.getString(0);
  217. ret += ",";
  218. }
  219. ret = ret.substring(0, ret.length() - 1);
  220. CloseCursor(cursor);
  221. CloseDB(db);
  222. return ret;
  223. }
  224. public static int get_count(SQLiteDatabase db, String table, String where)
  225. {
  226. int ret = 0;
  227. Cursor cursor = db.query(table, new String[] { "count(*)" }, where,
  228. null, null, null, null, null);
  229. if (cursor.moveToNext())
  230. {
  231. ret = cursor.getInt(0);
  232. }
  233. dbUtil.CloseCursor(cursor);
  234. return ret;
  235. }
  236. public static int get_recordid(SQLiteDatabase db, String table, String where)
  237. {
  238. return get_recordid(db, table, where, null);
  239. }
  240. public static int get_recordid(SQLiteDatabase db, String table,
  241. String where, String Orderby)
  242. {
  243. int ret = -1;
  244. Cursor cursor = null;
  245. try
  246. {
  247. cursor = db.query(table, FIELD_ID, where, null, null, null,
  248. Orderby, null);
  249. if (cursor.moveToNext())
  250. ret = cursor.getInt(0);
  251. else
  252. ret = -1;
  253. }
  254. catch (SQLiteException e)
  255. {
  256. Log.e("sql_err", e.toString());
  257. }
  258. finally
  259. {
  260. dbUtil.CloseCursor(cursor);
  261. }
  262. return ret;
  263. }
  264. // if(isTableEmpty(db,"block_records"))
  265. }