| 1226 | return this; |
| 1227 | } |
| 1228 | Long insert() |
| 1229 | { |
| 1230 | if ( fields.size()==0 ) |
| 1231 | return null; |
| 1232 | StringBuilder valueBuf = new StringBuilder(); |
| 1233 | try { |
| 1234 | String ignoreOption = ""; //"OR IGNORE "; |
| 1235 | StringBuilder buf = new StringBuilder("INSERT " + ignoreOption + " INTO "); |
| 1236 | buf.append(tableName); |
| 1237 | buf.append(" (id"); |
| 1238 | for ( String field : fields ) { |
| 1239 | buf.append(","); |
| 1240 | buf.append(field); |
| 1241 | } |
| 1242 | buf.append(") VALUES (NULL"); |
| 1243 | for ( String field : fields ) { |
| 1244 | buf.append(","); |
| 1245 | buf.append("?"); |
| 1246 | } |
| 1247 | buf.append(")"); |
| 1248 | String sql = buf.toString(); |
| 1249 | Log.d("cr3db", "going to execute " + sql); |
| 1250 | SQLiteStatement stmt = null; |
| 1251 | Long id = null; |
| 1252 | try { |
| 1253 | stmt = mDB.compileStatement(sql); |
| 1254 | for ( int i=1; i<=values.size(); i++ ) { |
| 1255 | Object v = values.get(i-1); |
| 1256 | valueBuf.append(v!=null ? v.toString() : "null"); |
| 1257 | valueBuf.append(","); |
| 1258 | if ( v==null ) |
| 1259 | stmt.bindNull(i); |
| 1260 | else if (v instanceof String) |
| 1261 | stmt.bindString(i, (String)v); |
| 1262 | else if (v instanceof Long) |
| 1263 | stmt.bindLong(i, (Long)v); |
| 1264 | else if (v instanceof Double) |
| 1265 | stmt.bindDouble(i, (Double)v); |
| 1266 | } |
| 1267 | id = stmt.executeInsert(); |
| 1268 | Log.d("cr3db", "added book, id=" + id + ", query=" + sql); |
| 1269 | } finally { |
| 1270 | if ( stmt!=null ) |
| 1271 | stmt.close(); |
| 1272 | } |
| 1273 | return id; |
| 1274 | } catch ( Exception e ) { |
| 1275 | Log.e("cr3db", "insert failed: " + e.getMessage()); |
| 1276 | Log.e("cr3db", "values: " + valueBuf.toString()); |
| 1277 | return null; |
| 1278 | } |
| 1279 | } |
| 1280 | boolean update( Long id ) |
| 1281 | { |
| 1282 | if ( fields.size()==0 ) |