当前位置: 首页>>代码示例>>Java>>正文


Java SQLiteOpenHelper.getWritableDatabase方法代码示例

本文整理汇总了Java中android.database.sqlite.SQLiteOpenHelper.getWritableDatabase方法的典型用法代码示例。如果您正苦于以下问题:Java SQLiteOpenHelper.getWritableDatabase方法的具体用法?Java SQLiteOpenHelper.getWritableDatabase怎么用?Java SQLiteOpenHelper.getWritableDatabase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.database.sqlite.SQLiteOpenHelper的用法示例。


在下文中一共展示了SQLiteOpenHelper.getWritableDatabase方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: deleteHistoryItem

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
public void deleteHistoryItem(int number) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();      
    cursor = db.query(DBHelper.TABLE_NAME,
                      ID_COL_PROJECTION,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");
    cursor.move(number + 1);
    db.delete(DBHelper.TABLE_NAME, DBHelper.ID_COL + '=' + cursor.getString(0), null);
  } finally {
    close(cursor, db);
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:17,代码来源:HistoryManager.java

示例2: addHistoryItem

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
public void addHistoryItem(Result result, ResultHandler handler) {
  // Do not save this item to the history if the preference is turned off, or the contents are
  // considered secure.
  if (!activity.getIntent().getBooleanExtra(Intents.Scan.SAVE_HISTORY, true) ||
      handler.areContentsSecure() || !enableHistory) {
    return;
  }

  SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
  if (!prefs.getBoolean(PreferencesActivity.KEY_REMEMBER_DUPLICATES, false)) {
    deletePrevious(result.getText());
  }

  ContentValues values = new ContentValues();
  values.put(DBHelper.TEXT_COL, result.getText());
  values.put(DBHelper.FORMAT_COL, result.getBarcodeFormat().toString());
  values.put(DBHelper.DISPLAY_COL, handler.getDisplayContents().toString());
  values.put(DBHelper.TIMESTAMP_COL, System.currentTimeMillis());

  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    // Insert the new entry into the DB.
    db.insert(DBHelper.TABLE_NAME, DBHelper.TIMESTAMP_COL, values);
  } finally {
    close(null, db);
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:30,代码来源:HistoryManager.java

示例3: deletePrevious

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
private void deletePrevious(String text) {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, DBHelper.TEXT_COL + "=?", new String[] { text });
  } finally {
    close(null, db);
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:11,代码来源:HistoryManager.java

示例4: buildHistory

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
/**
 * <p>Builds a text representation of the scanning history. Each scan is encoded on one
 * line, terminated by a line break (\r\n). The values in each line are comma-separated,
 * and double-quoted. Double-quotes within values are escaped with a sequence of two
 * double-quotes. The fields output are:</p>
 *
 * <ol>
 *  <li>Raw text</li>
 *  <li>Display text</li>
 *  <li>Format (e.g. QR_CODE)</li>
 *  <li>Unix timestamp (milliseconds since the epoch)</li>
 *  <li>Formatted version of timestamp</li>
 *  <li>Supplemental info (e.g. price info for a product barcode)</li>
 * </ol>
 */
CharSequence buildHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME,
                      COLUMNS,
                      null, null, null, null,
                      DBHelper.TIMESTAMP_COL + " DESC");

    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    StringBuilder historyText = new StringBuilder(1000);
    while (cursor.moveToNext()) {

      historyText.append('"').append(massageHistoryField(cursor.getString(0))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(1))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(2))).append("\",");
      historyText.append('"').append(massageHistoryField(cursor.getString(3))).append("\",");

      // Add timestamp again, formatted
      long timestamp = cursor.getLong(3);
      historyText.append('"').append(massageHistoryField(
          format.format(new Date(timestamp)))).append("\",");

      // Above we're preserving the old ordering of columns which had formatted data in position 5

      historyText.append('"').append(massageHistoryField(cursor.getString(4))).append("\"\r\n");
    }
    return historyText;
  } finally {
    close(cursor, db);
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:50,代码来源:HistoryManager.java

示例5: clearHistory

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
void clearHistory() {
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;
  try {
    db = helper.getWritableDatabase();      
    db.delete(DBHelper.TABLE_NAME, null, null);
  } finally {
    close(null, db);
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:11,代码来源:HistoryManager.java

示例6: init

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
public synchronized static void init(Context context, String databaseName) {
    SQLiteOpenHelper helper = new OrmSQLiteOpenHelper(context, databaseName, 1, null);
    sDatabase = helper.getWritableDatabase();
    if (sDatabase != null) {
        sDatabaseState = STATE_DATABASE_EXISTS;
    }
}
 
开发者ID:JackWHLiu,项目名称:jackknife,代码行数:8,代码来源:Orm.java

示例7: autoincrement_test

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
/**
 * Tests to ensure that inserts into your database results in automatically
 * incrementing row IDs.
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void autoincrement_test() throws Exception{

    /* First, let's ensure we have some values in our table initially */
    insert_single_record_test();

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    assertEquals("ID Autoincrement test failed!",
            firstRowId + 1, secondRowId);


}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:40,代码来源:DatabaseTest.java

示例8: createdb

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
public void createdb(View v) {
	CursorFactory factory = null;
	int version = 1;// �Զ�����1��ʼ
	String name = "db.db";// ���ݿ���
	Context Context = v.getContext();
	// ʹ��sqlliteopenhelpeʵ������ȡһ��sqldatabase
	SQLiteOpenHelper helper = new MySQLiteOpenHelper(Context, name,
			factory, version);
	SQLiteDatabase db = helper.getWritableDatabase();

}
 
开发者ID:z9961,项目名称:DoList,代码行数:12,代码来源:CreateDBActivity.java

示例9: insert_single_record_test

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
/**
 * This method tests inserting a single record into an empty table from a brand new database.
 * The purpose is to test that the database is working as expected
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void insert_single_record_test() throws Exception{

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* If the insert fails, database.insert returns -1 */
    assertNotEquals("Unable to insert into the database", -1, firstRowId);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */
    String emptyQueryError = "Error: No Records returned from waitlist query";
    assertTrue(emptyQueryError,
            wCursor.moveToFirst());

    /* Close cursor and database */
    wCursor.close();
    dbHelper.close();
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:58,代码来源:DatabaseTest.java

示例10: addHistoryItemDetails

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
public void addHistoryItemDetails(String itemID, String itemDetails) {
  // As we're going to do an update only we don't need need to worry
  // about the preferences; if the item wasn't saved it won't be udpated
  SQLiteOpenHelper helper = new DBHelper(activity);
  SQLiteDatabase db = null;    
  Cursor cursor = null;
  try {
    db = helper.getWritableDatabase();
    cursor = db.query(DBHelper.TABLE_NAME,
                      ID_DETAIL_COL_PROJECTION,
                      DBHelper.TEXT_COL + "=?",
                      new String[] { itemID },
                      null,
                      null,
                      DBHelper.TIMESTAMP_COL + " DESC",
                      "1");
    String oldID = null;
    String oldDetails = null;
    if (cursor.moveToNext()) {
      oldID = cursor.getString(0);
      oldDetails = cursor.getString(1);
    }

    if (oldID != null) {
      String newDetails;
      if (oldDetails == null) {
        newDetails = itemDetails;
      } else if (oldDetails.contains(itemDetails)) {
        newDetails = null;
      } else {
        newDetails = oldDetails + " : " + itemDetails;
      } 
      if (newDetails != null) {
        ContentValues values = new ContentValues();
        values.put(DBHelper.DETAILS_COL, newDetails);
        db.update(DBHelper.TABLE_NAME, values, DBHelper.ID_COL + "=?", new String[] { oldID });
      }
    }

  } finally {
    close(cursor, db);
  }
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:44,代码来源:HistoryManager.java

示例11: upgrade_database_test

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
/**
 * Tests that onUpgrade works by inserting 2 rows then calling onUpgrade and verifies that the
 * database has been successfully dropped and recreated by checking that the database is there
 * but empty
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void upgrade_database_test() throws Exception{

    /* Insert 2 rows before we upgrade to check that we dropped the database correctly */

    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();

    ContentValues testValues = new ContentValues();
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, "test name");
    testValues.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, 99);

    /* Insert ContentValues into database and get first row ID back */
    long firstRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    /* Insert ContentValues into database and get another row ID back */
    long secondRowId = database.insert(
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            null,
            testValues);

    dbHelper.onUpgrade(database, 0, 1);
    database = dbHelper.getReadableDatabase();

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    assertTrue(tableNameCursor.getCount() == 1);

    /*
     * Query the database and receive a Cursor. A Cursor is the primary way to interact with
     * a database in Android.
     */
    Cursor wCursor = database.query(
            /* Name of table on which to perform the query */
            WaitlistContract.WaitlistEntry.TABLE_NAME,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Columns to group by */
            null,
            /* Columns to filter by row groups */
            null,
            /* Sort order to return in Cursor */
            null);

    /* Cursor.moveToFirst will return false if there are no records returned from your query */

    assertFalse("Database doesn't seem to have been dropped successfully when upgrading",
            wCursor.moveToFirst());

    tableNameCursor.close();
    database.close();
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:74,代码来源:DatabaseTest.java

示例12: create_database_test

import android.database.sqlite.SQLiteOpenHelper; //导入方法依赖的package包/类
/**
 * This method tests that our database contains all of the tables that we think it should
 * contain.
 * @throws Exception in case the constructor hasn't been implemented yet
 */
@Test
public void create_database_test() throws Exception{


    /* Use reflection to try to run the correct constructor whenever implemented */
    SQLiteOpenHelper dbHelper =
            (SQLiteOpenHelper) mDbHelperClass.getConstructor(Context.class).newInstance(mContext);

    /* Use WaitlistDbHelper to get access to a writable database */
    SQLiteDatabase database = dbHelper.getWritableDatabase();


    /* We think the database is open, let's verify that here */
    String databaseIsNotOpen = "The database should be open and isn't";
    assertEquals(databaseIsNotOpen,
            true,
            database.isOpen());

    /* This Cursor will contain the names of each table in our database */
    Cursor tableNameCursor = database.rawQuery(
            "SELECT name FROM sqlite_master WHERE type='table' AND name='" +
                    WaitlistContract.WaitlistEntry.TABLE_NAME + "'",
            null);

    /*
     * If tableNameCursor.moveToFirst returns false from this query, it means the database
     * wasn't created properly. In actuality, it means that your database contains no tables.
     */
    String errorInCreatingDatabase =
            "Error: This means that the database has not been created correctly";
    assertTrue(errorInCreatingDatabase,
            tableNameCursor.moveToFirst());

    /* If this fails, it means that your database doesn't contain the expected table(s) */
    assertEquals("Error: Your database was created without the expected tables.",
            WaitlistContract.WaitlistEntry.TABLE_NAME, tableNameCursor.getString(0));

    /* Always close a cursor when you are done with it */
    tableNameCursor.close();
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:46,代码来源:DatabaseTest.java


注:本文中的android.database.sqlite.SQLiteOpenHelper.getWritableDatabase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。