public void CarregaLista(String searched) { SQLiteDatabase myDB = null; try { myDB = this.openOrCreateDatabase("skyzone", MODE_PRIVATE, null); Cursor mCursor = myDB.rawQuery("SELECT * FROM Zones WHERE Name LIKE '" + searched + "%';", null); startManagingCursor(mCursor); ListAdapter adapter = new SimpleCursorAdapter( this, R.layout.list, mCursor, new String[] {"Icon", "Name", "Country", "State", "City", "Phone"}, new int[] {R.id.Icon, R.id.Name, R.id.Country, R.id.State, R.id.City, R.id.Phone}); this.setListAdapter(adapter); this.getListView().setTextFilterEnabled(true); } finally { if (myDB != null) { myDB.close(); } } }
/** * Close a database. * * @param dbName The name of the database-NOT including its extension. */ private void closeDatabase(String dbName) { SQLiteDatabase mydb = this.getDatabase(dbName); if (mydb != null) { mydb.close(); dbmap.remove(dbName); } }
/** Close any open database object. */ public synchronized void close() { if (mIsInitializing) throw new IllegalStateException("Closed during initialization"); if (mDatabase != null && mDatabase.isOpen()) { mDatabase.close(); mDatabase = null; } }
public void deleteTran(Account acct, String transID) { String[] args = {Integer.toString(acct.ID), transID}; db.beginTransaction(); try { db.delete("trans", "acct_id=? and trans_id=?", args); db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
private void updateAccount(Account acct) { db.beginTransaction(); try { ContentValues newValue = acctValues(acct); String[] args = {Integer.toString(acct.ID)}; db.update("acct", newValue, "_id=?", args); db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
private void updateTran(Transaction trans) { db.beginTransaction(); try { ContentValues newValue = tranValues(trans); String[] args = {Integer.toString(trans.ID)}; db.update("trans", newValue, "_id=?", args); db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
public void onPressClear(View view) { // Do something in response to button DataBaseHelper databasehelper = new DataBaseHelper(this); SQLiteDatabase db = databasehelper.getWritableDatabase(); if (db != null) { // Se elimina la versión anterior de la tabla db.execSQL("DROP TABLE IF EXISTS " + databasehelper.TABLA); // Se utiliza para poder crear la nueva base de datos databasehelper.onCreate(db); } }
// metodo para insertar a la tabla private void insertPrso(String nombre, String ciudad) { DataBaseHelper databasehelper = new DataBaseHelper(this); SQLiteDatabase db = databasehelper.getWritableDatabase(); if (db != null) { ContentValues cv = new ContentValues(); cv.put(DataBaseHelper.NOMBRE, nombre); cv.put(DataBaseHelper.CIUDAD, ciudad); db.insert("tabla", DataBaseHelper.NOMBRE, cv); db.close(); } }
private int addAccount(Account acct) { db.beginTransaction(); int acct_id; try { ContentValues newValue = acctValues(acct); acct_id = (int) db.insertOrThrow("acct", "name", newValue); acct.ID = acct_id; db.setTransactionSuccessful(); } finally { db.endTransaction(); } return acct_id; }
private int addTran(Transaction trans) { db.beginTransaction(); int tran_id; try { ContentValues newValue = tranValues(trans); tran_id = (int) db.insertOrThrow("trans", "acct_id", newValue); trans.ID = tran_id; db.setTransactionSuccessful(); } finally { db.endTransaction(); } return tran_id; }
/** * Enables or disables the use of write-ahead logging for the database. * * <p>Write-ahead logging cannot be used with read-only databases so the value of this flag is * ignored if the database is opened read-only. * * @param enabled True if write-ahead logging should be enabled, false if it should be disabled. * @see SQLiteDatabase#enableWriteAheadLogging() */ public void setWriteAheadLoggingEnabled(boolean enabled) { synchronized (this) { if (mEnableWriteAheadLogging != enabled) { if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) { if (enabled) { mDatabase.enableWriteAheadLogging(); } else { mDatabase.disableWriteAheadLogging(); } } mEnableWriteAheadLogging = enabled; } } }
public Cursor acctList(String constraint) { if (constraint == null) { return db.query("acct", AC_COLS, null, null, null, null, "name"); } else { return db.query( "acct", AC_COLS, "name like ?", new String[] {"%" + constraint + "%"}, null, null, "name"); } }
/** * 单个数据查询 2 * * @param sql 传入sql语句直接查询 * @return 单个数据指针 */ public Cursor get(String sql) { Cursor mCursor = db.rawQuery(sql, null); // 如果指针存在,就把指针移到第一个条目上 if (mCursor != null) mCursor.moveToFirst(); return mCursor; }
public static SQLiteDebug.PagerStats getDatabaseInfo() { SQLiteDebug.PagerStats localPagerStats = new SQLiteDebug.PagerStats(); getPagerStats(localPagerStats); localPagerStats.dbStats = SQLiteDatabase.getDbStats(); return localPagerStats; }
private static Cursor getMesesDiferentes() throws SQLException { SQLiteDatabase conn = null; Cursor rs = null; String consulta = " Select distinct month from session "; try { conn = BaseDatos.getConn(); rs = conn.rawQuery(consulta, null); } catch (SQLException e) { throw (e); } return rs; }
// 获取某个数据库的表的名称 public String getTablesList() { Cursor c = db.rawQuery("select name from sqlite_master where type='table' order by name", null); String str = ""; while (c.moveToNext()) { str += c.getString(0) + "\n"; } return "表的名称为:\n" + str; }
// 删除一个表 public void dropTable(String table) { try { db.execSQL("drop table if exists " + table); } catch (SQLException e) { Toast.makeText(mCtx, "数据表删除失败", Toast.LENGTH_LONG).show(); } }
private boolean tranExists(Transaction trans) { String[] args = {Integer.toString(trans.acct.ID), trans.transID}; Cursor cur = db.query("trans", ID_COLS, "acct_id=? and trans_id=?", args, null, null, null); try { return cur.moveToNext(); } finally { cur.close(); } }
private void setMaxConnectionPoolSizeLocked() { if (!SQLiteDatabase.hasCodec() && (mConfiguration.openFlags & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0) { mMaxConnectionPoolSize = SQLiteGlobal.getWALConnectionPoolSize(); } else { // TODO: We don't actually need to restrict the connection pool size to 1 // for non-WAL databases. There might be reasons to use connection pooling // with other journal modes. For now, enabling connection pooling and // using WAL are the same thing in the API. mMaxConnectionPoolSize = 1; } }
/** * 列表查询用 1 * * @param tableName 表名 * @param strCols 列名 * @param strWhere 条件 * @param strGroupby 分组 * @param strOrderby 排序 * @return 数据指针 */ public Cursor getAll( String tableName, String[] strCols, String strWhere, String strGroupby, String strOrderby) { return db.query( tableName, // 表名 strCols, // 列 strWhere, // where语句 null, // where语句的参数 strGroupby, // GROUP by语句 null, // HAVING语句 strOrderby // order by语句 ); }
public Account getAccount(int ID) { Account acct = null; String[] args = {Integer.toString(ID)}; Cursor cur = db.query("acct", AC_COLS, "_id=?", args, null, null, null); try { if (!cur.moveToNext()) return null; acct = getAccountFromCursor(cur); } finally { cur.close(); } return acct; }
public void establishAccount(ServiceAcctInfo acct) { String[] args = {Integer.toString(acct.ID)}; Cursor cur = db.query("acct", AC_COLS, "service_id=?", args, null, null, null); try { if (cur.moveToNext()) return; } finally { cur.close(); } Account newAcct = new Account(); newAcct.serviceId = acct.ID; newAcct.name = acct.desc; addAccount(newAcct); }
public Cursor queryByparams( String[] columns, String selection, String table, String[] selectionArgs) { // 第一个参数String:表名 // 第二个参数String[]:要查询的列名 // 第三个参数String:查询条件 // 第四个参数String[]:查询条件的参数 // 第五个参数String:对查询的结果进行分组 // 第六个参数String:对分组的结果进行限制 // 第七个参数String:对查询的结果进行排序 String groupBy = null; String having = null; String orderBy = null; return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy); }
/** * 单个数据查询 1 * * @param rowId 数据id * @param tableName 表名 * @param key 关键字 * @param strCols 列名 * @param strWhere 条件 * @param strGroupby 分组 * @param strOrderby 排序 * @return 单个数据指针 */ public Cursor get( long rowId, String tableName, String key, String[] strCols, String strWhere, String strGroupby, String strOrderby) { Cursor mCursor = db.query(tableName, strCols, key + "=" + rowId, null, strGroupby, null, strOrderby); // 如果指针存在,就把指针移到第一个条目上 if (mCursor != null) mCursor.moveToFirst(); return mCursor; }
/** * Open a database. * * @param dbname The name of the database-NOT including its extension. * @param password The database password or null. */ private void openDatabase(String dbname, String password) { if (this.getDatabase(dbname) != null) this.closeDatabase(dbname); String completeDBName = dbname + ".db"; File dbfile = this.cordova.getActivity().getDatabasePath(dbname + ".db"); if (!dbfile.exists()) copyPrepopulatedDatabase(completeDBName, dbfile); if (!dbfile.exists()) { dbfile.getParentFile().mkdirs(); } Log.v("info", "Open sqlite db: " + dbfile.getAbsolutePath()); SQLiteDatabase mydb = SQLiteDatabase.openOrCreateDatabase(dbfile, null); dbmap.put(dbname, mydb); }
/** * Delete a database. * * @param dbname The name of the database-NOT including its extension. * @return true if successful or false if an exception was encountered */ private boolean deleteDatabase(String dbname) { boolean status = false; // assume the worst case: if (this.getDatabase(dbname) != null) this.closeDatabase(dbname); File dbfile = this.cordova.getActivity().getDatabasePath(dbname + ".db"); Log.v("info", "delete sqlite db: " + dbfile.getAbsolutePath()); // Use try & catch just in case android.os.Build.VERSION.SDK_INT >= 16 was lying: try { status = SQLiteDatabase.deleteDatabase(dbfile); } catch (Exception ex) { // log & give up: Log.v("executeSqlBatch", "deleteDatabase(): Error=" + ex.getMessage()); ex.printStackTrace(); } return status; }
/** * Create and/or open a database. This will be the same object returned by {@link * #getWritableDatabase} unless some problem, such as a full disk, requires the database to be * opened read-only. In that case, a read-only database object will be returned. If the problem is * fixed, a future call to {@link #getWritableDatabase} may succeed, in which case the read-only * database object will be closed and the read/write object will be returned in the future. * * <p class="caution">Like {@link #getWritableDatabase}, this method may take a long time to * return, so you should not call it from the application main thread, including from {@link * android.content.ContentProvider#onCreate ContentProvider.onCreate()}. * * @throws SQLiteException if the database cannot be opened * @return a database object valid until {@link #getWritableDatabase} or {@link #close} is called. */ public synchronized SQLiteDatabase getReadableDatabase() { if (mDatabase != null) { if (!mDatabase.isOpen()) { // darn! the user closed the database by calling mDatabase.close() mDatabase = null; } else { return mDatabase; // The database is already open for business } } if (mIsInitializing) { throw new IllegalStateException("getReadableDatabase called recursively"); } try { return getWritableDatabase(); } catch (SQLiteException e) { if (mName == null) throw e; // Can't open a temp database read-only! Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e); } SQLiteDatabase db = null; try { mIsInitializing = true; String path = mContext.getDatabasePath(mName).getPath(); db = SQLiteDatabase.openDatabase( path, mPasswd, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler); if (db.getVersion() != mNewVersion) { throw new SQLiteException( "Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + path); } onOpen(db); Log.w(TAG, "Opened " + mName + " in read-only mode"); mDatabase = db; return mDatabase; } finally { mIsInitializing = false; if (db != null && db != mDatabase) db.close(); } }
public void close() { db.close(); }
public void wipeTable(SQLiteDatabase db) { for (String cmd : DROP_TABLE) { db.execSQL(cmd); } onCreate(db); }
@Override public void onCreate(SQLiteDatabase db) { for (String cmd : CREATE_TABLE) { db.execSQL(cmd); } }