如何正确的把数据插入到数据库中

48次阅读
没有评论

共计 3099 个字符,预计需要花费 8 分钟才能阅读完成。

这篇文章给大家介绍如何正确的把数据插入到数据库中,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

把数据放入数据库

通过把 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);

关于如何正确的把数据插入到数据库中就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-18发表,共计3099字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)