现在的位置: 首页 > 数据库 > 正文

Android数据库操作-正确的把数据插入到数据库中(2)

2019年09月09日 数据库 ⁄ 共 2982字 ⁄ 字号 评论关闭

把数据放入数据库

通过把ContentValues对象传入instert()方法把数据插入数据库:

// Gets the data repository in write mode 
SQLiteDatabase
 db = mDbHelper.getWritableDatabase(); 
 
// Create a new map of values, where column names are the keys 
ContentValues values = new ContentValues(); 
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id); 
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title); 
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, content); 
 
// Insert the new row, returning the primary key value of the new row 
long newRowId; 
newRowId = db.insert( 
         FeedReaderContract.FeedEntry.TABLE_NAME, 
         FeedReaderContract.FeedEntry.COLUMN_NAME_NULLABLE, 
         values);

insert()方法的第一个参数是表名。第二个参数提供了框架中的一个列名,在ContentValues的值是空的时候,框架会向表中插入NULL值(如果这个参数是“null”,那么当没有值时,框架不会向表中插入一行。

 

从数据库中读取数据

要从数据库中读取数据,就要使用query()方法,你需要给这个方法传入选择条件和你想要获取数据的列。查询结果会在Cursor对象中被返回。

SQLiteDatabase db = mDbHelper.getReadableDatabase(); 
 

// Define a projection that specifies which columns from the database 
// you will actually use after this query. 
String[] projection = { 
    FeedReaderContract.FeedEntry._ID, 
    FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, 
    FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED, 
    ... 
    }; 
 
// How you want the results sorted in the resulting Cursor 
String sortOrder = 
    FeedReaderContract.FeedEntry.COLUMN_NAME_UPDATED + " DESC"; 
 
Cursor c = db.query( 
    FeedReaderContract.FeedEntry.TABLE_NAME,  // The table to query 
    projection,                               // The columns to return 
    selection,                                // The columns for the WHERE clause 
    selectionArgs,                            // The values for the WHERE clause 
    null,                                     // don't group the rows 
    null,                                     // don't filter by row groups 
    sortOrder                                 // The sort order 
    );

 

使用Cursor对象的移动方法来查看游标中的一行数据,在开始读取数据之前必须先调用这个方法。通常,应该从调用moveToFirst()方法开始,它会把读取数据的位置放到结果集中第一实体。对于每一行,你可以通过调用Cursor对象的相应的get方法来读取列的值,如果getString()getLong()方法。对于每个get方法,你必须把你希望的列的索引位置传递给它,你可以通过调用getColumnIndex()getColumnIndexOrThrow()方法来获取列的索引。例如:

cursor.moveToFirst(); 
long
 itemId = cursor.getLong( 
    cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID) 
);

 

从数据库中删除数据

要从一个表中删除行数据,你需要提供标识行的选择条件。数据API为创建选择条件提供了一种机制,它会防止SQL注入。这中机制把选择条件分成了选择条件和选择参数。条件子句定义了要查看的列,并且还允许你使用组合列来进行筛选。参数是用于跟条件绑定的、用户筛选数据的值。因为这样不会导致像SQL语句一样的处理,所以它避免了SQL注入。

// Define 'where' part of query. 
String
 selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?"; 
// Specify arguments in placeholder order. 
String[] selelectionArgs = { String.valueOf(rowId) }; 
// Issue SQL statement. 
db.delete(table_name, selection, selectionArgs);

 

更新数据库

当你需要编辑数据库值的时候,请使用update()方法。

这个方法在更新数据时会把insert()方法中内容值的语法跟delete()方法中的where语法结合在一起。

SQLiteDatabase db = mDbHelper.getReadableDatabase(); 
 

// New value for one column 
ContentValues values = new ContentValues(); 
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, title); 
 
// Which row to update, based on the ID 
String selection = FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?"; 
String[] selelectionArgs = { String.valueOf(rowId) }; 
 
int count = db.update( 
    FeedReaderDbHelper.FeedEntry.TABLE_NAME, 
    values, 
    selection, 
    selectionArgs);

 

抱歉!评论已关闭.