|
1 |
| -package com.devsmart.android; |
2 |
| - |
3 |
| -import java.io.IOException; |
4 |
| -import java.util.Iterator; |
5 |
| -import java.util.Map; |
6 |
| -import java.util.Map.Entry; |
7 |
| - |
8 |
| -import android.content.ContentValues; |
9 |
| -import android.content.Context; |
10 |
| -import android.content.res.Resources; |
11 |
| -import android.database.Cursor; |
12 |
| -import android.database.sqlite.SQLiteDatabase; |
13 |
| -import android.database.sqlite.SQLiteQueryBuilder; |
14 |
| -import android.database.sqlite.SQLiteStatement; |
15 |
| -import android.net.Uri; |
16 |
| -import android.util.Log; |
17 |
| - |
18 |
| -public class DbUtils { |
19 |
| - |
20 |
| - public static String getStringCursorValue(Cursor cursor, String columnName, String defaultValue) { |
21 |
| - String retval = null; |
22 |
| - int columnNum = cursor.getColumnIndex(columnName); |
23 |
| - if(columnNum == -1){ |
24 |
| - retval = defaultValue; |
25 |
| - } else { |
26 |
| - retval = cursor.getString(columnNum); |
27 |
| - } |
28 |
| - return retval; |
29 |
| - } |
30 |
| - |
31 |
| - public static Long getLongCursorValue(Cursor cursor, String columnName, Long defaultValue){ |
32 |
| - Long retval = defaultValue; |
33 |
| - int columnNum = cursor.getColumnIndex(columnName); |
34 |
| - if(columnNum == -1){ |
35 |
| - retval = defaultValue; |
36 |
| - } else { |
37 |
| - retval = cursor.getLong(columnNum); |
38 |
| - } |
39 |
| - return retval; |
40 |
| - } |
41 |
| - |
42 |
| - public static Integer getIntCursorValue(Cursor cursor, String columnName, Integer defaultvalue) { |
43 |
| - Integer retval = defaultvalue; |
44 |
| - int columnNum = cursor.getColumnIndex(columnName); |
45 |
| - if(columnNum == -1){ |
46 |
| - retval = defaultvalue; |
47 |
| - } else { |
48 |
| - retval = cursor.getInt(columnNum); |
49 |
| - } |
50 |
| - return retval; |
51 |
| - } |
52 |
| - |
53 |
| - public static String lookupSingleStringValue(Context context, Uri uri, String columnName, String defaultValue) { |
54 |
| - String retval = defaultValue; |
55 |
| - Cursor cursor = context.getContentResolver().query(uri, new String[]{columnName}, null, null, null); |
56 |
| - try { |
57 |
| - if(cursor.moveToFirst()){ |
58 |
| - retval = cursor.getString(0); |
59 |
| - } |
60 |
| - } finally { |
61 |
| - cursor.close(); |
62 |
| - } |
63 |
| - |
64 |
| - return retval; |
65 |
| - } |
66 |
| - |
67 |
| - public static String lookupSingleStringValue(SQLiteDatabase db, String columnName, String table, String selection, String[] selectionArgs, String defaultValue) { |
68 |
| - String retval = defaultValue; |
69 |
| - Cursor c = db.query(table, new String[]{columnName}, selection, selectionArgs, null, null, null); |
70 |
| - try { |
71 |
| - if(c.moveToFirst()){ |
72 |
| - retval = c.getString(0); |
73 |
| - } |
74 |
| - } finally { |
75 |
| - c.close(); |
76 |
| - } |
77 |
| - return retval; |
78 |
| - } |
79 |
| - |
80 |
| - public static void executeAssetSQL(SQLiteDatabase db, Resources resources, String assetPath){ |
81 |
| - db.beginTransaction(); |
82 |
| - try { |
83 |
| - String createSQL = StringUtils.loadAssetString(resources, assetPath); |
84 |
| - |
85 |
| - for(String sql : createSQL.split(";")){ |
86 |
| - sql = sql.trim(); |
87 |
| - if(sql.length() > 0){ |
88 |
| - db.execSQL(sql); |
89 |
| - } |
90 |
| - } |
91 |
| - db.setTransactionSuccessful(); |
92 |
| - } catch (IOException e) { |
93 |
| - Log.e(DbUtils.class.getName(), "Error executing sql statement", e); |
94 |
| - } finally { |
95 |
| - db.endTransaction(); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - public static long updateOrInsert(SQLiteDatabase db, String table, ContentValues values, String selection, String[] selectionArgs) { |
100 |
| - long retval = -1; |
101 |
| - db.beginTransaction(); |
102 |
| - try { |
103 |
| - retval = db.update(table, values, selection, selectionArgs); |
104 |
| - if(retval == 0){ |
105 |
| - retval = db.insert(table, null, values); |
106 |
| - } |
107 |
| - db.setTransactionSuccessful(); |
108 |
| - } finally { |
109 |
| - db.endTransaction(); |
110 |
| - } |
111 |
| - return retval; |
112 |
| - } |
113 |
| - |
114 |
| - /* |
115 |
| - public static long updateOrReplaceRow(SQLiteDatabase db, ContentValues values, String table, String selection, String[] selectionArgs) { |
116 |
| - String query = "UPDATE OR REPLACE %s SET "; |
117 |
| - |
118 |
| - Iterator<Entry<String, Object>> it = values.valueSet().iterator(); |
119 |
| - while(it.hasNext()){ |
120 |
| - Entry<String, Object> entry = it.next(); |
121 |
| - query += String.format("%s = '%s'", entry.getKey(), entry.getValue().toString()); |
122 |
| - if(it.hasNext()){ |
123 |
| - query += ", "; |
124 |
| - } |
125 |
| - } |
126 |
| - |
127 |
| - query += " WHERE"; |
128 |
| - |
129 |
| - selection.rep |
130 |
| - String.format(selection, selectionArgs)); |
131 |
| -
|
132 |
| - } |
133 |
| - */ |
134 |
| - |
135 |
| -} |
| 1 | +package com.devsmart.android; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Map.Entry; |
| 7 | + |
| 8 | +import android.content.ContentValues; |
| 9 | +import android.content.Context; |
| 10 | +import android.content.res.Resources; |
| 11 | +import android.database.Cursor; |
| 12 | +import android.database.sqlite.SQLiteDatabase; |
| 13 | +import android.database.sqlite.SQLiteQueryBuilder; |
| 14 | +import android.database.sqlite.SQLiteStatement; |
| 15 | +import android.net.Uri; |
| 16 | +import android.util.Log; |
| 17 | + |
| 18 | +public class DbUtils { |
| 19 | + |
| 20 | + public static String getStringCursorValue(Cursor cursor, String columnName, String defaultValue) { |
| 21 | + String retval = null; |
| 22 | + int columnNum = cursor.getColumnIndex(columnName); |
| 23 | + if(columnNum == -1){ |
| 24 | + retval = defaultValue; |
| 25 | + } else { |
| 26 | + retval = cursor.getString(columnNum); |
| 27 | + } |
| 28 | + return retval; |
| 29 | + } |
| 30 | + |
| 31 | + public static Long getLongCursorValue(Cursor cursor, String columnName, Long defaultValue){ |
| 32 | + Long retval = defaultValue; |
| 33 | + int columnNum = cursor.getColumnIndex(columnName); |
| 34 | + if(columnNum == -1){ |
| 35 | + retval = defaultValue; |
| 36 | + } else { |
| 37 | + retval = cursor.getLong(columnNum); |
| 38 | + } |
| 39 | + return retval; |
| 40 | + } |
| 41 | + |
| 42 | + public static Integer getIntCursorValue(Cursor cursor, String columnName, Integer defaultvalue) { |
| 43 | + Integer retval = defaultvalue; |
| 44 | + int columnNum = cursor.getColumnIndex(columnName); |
| 45 | + if(columnNum == -1){ |
| 46 | + retval = defaultvalue; |
| 47 | + } else { |
| 48 | + retval = cursor.getInt(columnNum); |
| 49 | + } |
| 50 | + return retval; |
| 51 | + } |
| 52 | + |
| 53 | + public static String lookupSingleStringValue(Context context, Uri uri, String columnName, String defaultValue) { |
| 54 | + String retval = defaultValue; |
| 55 | + Cursor cursor = context.getContentResolver().query(uri, new String[]{columnName}, null, null, null); |
| 56 | + try { |
| 57 | + if(cursor.moveToFirst()){ |
| 58 | + retval = cursor.getString(0); |
| 59 | + } |
| 60 | + } finally { |
| 61 | + cursor.close(); |
| 62 | + } |
| 63 | + |
| 64 | + return retval; |
| 65 | + } |
| 66 | + |
| 67 | + public static String lookupSingleStringValue(SQLiteDatabase db, String columnName, String table, String selection, String[] selectionArgs, String defaultValue) { |
| 68 | + String retval = defaultValue; |
| 69 | + Cursor c = db.query(table, new String[]{columnName}, selection, selectionArgs, null, null, null); |
| 70 | + try { |
| 71 | + if(c.moveToFirst()){ |
| 72 | + retval = c.getString(0); |
| 73 | + } |
| 74 | + } finally { |
| 75 | + c.close(); |
| 76 | + } |
| 77 | + return retval; |
| 78 | + } |
| 79 | + |
| 80 | + public static void executeAssetSQL(SQLiteDatabase db, Resources resources, String assetPath){ |
| 81 | + db.beginTransaction(); |
| 82 | + try { |
| 83 | + String createSQL = StringUtils.loadAssetString(resources, assetPath); |
| 84 | + |
| 85 | + for(String sql : createSQL.split("\\?")){ |
| 86 | + sql = sql.trim(); |
| 87 | + if(!StringUtils.isEmptyString(sql)){ |
| 88 | + db.execSQL(sql); |
| 89 | + } |
| 90 | + } |
| 91 | + db.setTransactionSuccessful(); |
| 92 | + } catch (IOException e) { |
| 93 | + Log.e(DbUtils.class.getName(), "Error executing sql statement", e); |
| 94 | + } finally { |
| 95 | + db.endTransaction(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static long updateOrInsert(SQLiteDatabase db, String table, ContentValues values, String selection, String[] selectionArgs) { |
| 100 | + long retval = -1; |
| 101 | + db.beginTransaction(); |
| 102 | + try { |
| 103 | + retval = db.update(table, values, selection, selectionArgs); |
| 104 | + if(retval == 0){ |
| 105 | + retval = db.insert(table, null, values); |
| 106 | + } |
| 107 | + db.setTransactionSuccessful(); |
| 108 | + } finally { |
| 109 | + db.endTransaction(); |
| 110 | + } |
| 111 | + return retval; |
| 112 | + } |
| 113 | + |
| 114 | + /* |
| 115 | + public static long updateOrReplaceRow(SQLiteDatabase db, ContentValues values, String table, String selection, String[] selectionArgs) { |
| 116 | + String query = "UPDATE OR REPLACE %s SET "; |
| 117 | + |
| 118 | + Iterator<Entry<String, Object>> it = values.valueSet().iterator(); |
| 119 | + while(it.hasNext()){ |
| 120 | + Entry<String, Object> entry = it.next(); |
| 121 | + query += String.format("%s = '%s'", entry.getKey(), entry.getValue().toString()); |
| 122 | + if(it.hasNext()){ |
| 123 | + query += ", "; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + query += " WHERE"; |
| 128 | + |
| 129 | + selection.rep |
| 130 | + String.format(selection, selectionArgs)); |
| 131 | +
|
| 132 | + } |
| 133 | + */ |
| 134 | + |
| 135 | +} |
0 commit comments