SQLiteRawUtil.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.xzjmyk.pm.activity.db;
  2. import android.database.Cursor;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.text.TextUtils;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * 对原生SQL语句的一些支持
  9. *
  10. * @author dty
  11. *
  12. */
  13. public class SQLiteRawUtil {
  14. public static final String CHAT_MESSAGE_TABLE_PREFIX = "msg_";
  15. public static String getCreateChatMessageTableSql(String tableName) {
  16. String sql = "CREATE TABLE IF NOT EXISTS "
  17. + tableName
  18. + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,type INTEGER NOT NULL,timeSend INTEGER NOT NULL,packetId VARCHAR NOT NULL,timeReceive INTEGER,fromUserId VARCHAR,fromUserName VARCHAR,isMySend SMALLINT,content VARCHAR,filePath VARCHAR,location_y VARCHAR,location_x VARCHAR,isRead SMALLINT,isUpload SMALLINT,isDownload SMALLINT,messageState INTEGER,timeLen INTEGER , fileSize INTEGER,objectId VARCHAR,sipStatus INTEGER,sipDuration INTEGER,cardId VARCHAR)";
  19. return sql;
  20. }
  21. public static void createTableIfNotExist(SQLiteDatabase db, String tableName, String createTableSql) {
  22. if (isTableExist(db, tableName)) {
  23. return;
  24. }
  25. db.execSQL(createTableSql);
  26. }
  27. public static boolean isTableExist(SQLiteDatabase db, String tableName) {
  28. boolean result = false;
  29. if (TextUtils.isEmpty(tableName.trim())) {
  30. return false;
  31. }
  32. Cursor cursor = null;
  33. try {
  34. String sql = "select count(*) as c from Sqlite_master where type ='table' and name ='" + tableName.trim() + "' ";
  35. cursor = db.rawQuery(sql, null);
  36. if (cursor.moveToNext()) {
  37. int count = cursor.getInt(0);
  38. if (count > 0) {
  39. result = true;
  40. }
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. } finally {
  45. if (cursor != null) {
  46. cursor.close();
  47. }
  48. }
  49. return result;
  50. }
  51. public static void dropTable(SQLiteDatabase db, String tableName) {
  52. String sql = "drop table " + tableName;
  53. db.execSQL(sql);
  54. }
  55. /**
  56. * 获取当前用户的消息表
  57. *
  58. * @param db
  59. * @param
  60. * @return
  61. */
  62. public static List<String> getUserChatMessageTables(SQLiteDatabase db, String ownerId) {
  63. String tablePrefix = CHAT_MESSAGE_TABLE_PREFIX + ownerId;
  64. Cursor cursor = null;
  65. try {
  66. String sql = "select name from Sqlite_master where type ='table' and name like '" + tablePrefix + "%'";
  67. cursor = db.rawQuery(sql, null);
  68. if (cursor != null) {
  69. List<String> tables = new ArrayList<String>();
  70. while (cursor.moveToNext()) {
  71. String name = cursor.getString(0);
  72. if (!TextUtils.isEmpty(name)) {
  73. tables.add(name);
  74. }
  75. }
  76. return tables;
  77. }
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (cursor != null) {
  82. cursor.close();
  83. }
  84. }
  85. return null;
  86. }
  87. }