SQLite 数据库 *** 作

SQLite 数据库 *** 作,第1张

概述     1.创建合同类(contract class)---是一个常量的容器,定义表、列、URIs的名字。 public final class FeedReaderContract { // To prevent someone from accidentally instantiating the contract class, // give it an empty constructor 1.创建合同类(contract class)---是一个常量的容器,定义表、列、URIs的名字。
public finalclass FeedReaderContract{ // To prevent someone from accIDentally instantiating the contract class,// give it an empty constructor.FeedReaderContract(){}/* Inner class that defines the table contents */staticabstractFeedEntryimplementsBaseColumnsString table_name ="entry"; ColUMN_name_ENTRY_ID "entryID" ColUMN_name_Title "Title" ColUMN_name_SUBTitle "subTitle"...}}
在合同类中为每张表创建内部类并列举其列。通过实现BaseColumns接口,该内部类会继承一个主键字段"_ID"。它不是必须的,但可以帮你的数据库与AndroID框架和谐的工作。
2.构造创建表和删除表的语句
private TEXT_TYPE " TEXT" COMMA_SEP "," sql_CREATE_ENTRIES "CREATE table "+FeedEntry.table_name " ("_ID " INTEGER PRIMARY KEY,"ColUMN_name_ENTRY_ID ColUMN_name_Title // Any other options for the CREATE command" )" sql_DELETE_ENTRIES "DROP table IF EXISTS "table_name;
3.创建 SQLiteOpenHelper 的子类,重写onCreate(),onUpgrade(),onopen()等回调函数。
FeedReaderDbHelperextendssqliteOpenHelper// If you change the database schema,you must increment the database version.int DATABASE_VERSION 1 DATABASE_name "FeedReader.db"FeedReaderDbHelper(Context context)super(context,0)"> DATABASE_namenull DATABASE_VERSION);voID onCreatesqliteDatabase dbexecsqlsql_CREATE_ENTRIES onUpgrade oldVersion newVersion// This database is only a cache for online data,so its upgrade policy is// to simply to discard the data and start oversql_DELETE_ENTRIESdb onDowngrade}
访问数据库前,先实例化 SQLiteOpenHelper子类:
 mDbHelper newgetContext());
4.向数据库中插入数据
// Gets the data repository in write mode db  mDbHelpergetWritableDatabase();// Create a new map of values,where column names are the keysContentValues values ContentValues valuesputColUMN_name_ENTRY_ID IDColUMN_name_Title TitleColUMN_name_CONTENT content// Insert the new row,returning the primary key value of the new rowlong newRowID newRowID insertColUMN_name_NulLABLE);
第二个参数提供当ContentValues是空的情况,框架(framework )可以插入NulL的列的名称。当你设置为null的时候,当 ContentValues没有任何值时框架不会插入数据。
5.查询数据库中的数据
getReadableDatabase// define a projection that specifIEs which columns from the database// you will actually use after this query.String[] projection _IDColUMN_name_UPDATED};// How you want the results sorted in the resulting Cursor sortOrder ColUMN_name_UPDATED " DESC"Cursor c query// The table to query projection// The columns to return selection// The columns for the WHERE clause selectionArgs// The values for the WHERE clause// don't group the rows// don't filter by row groups sortOrder // The sort order);
查询结果返回一个Cursor(游标),调用Cursor的方法movetoFirst()移动游标到第一个查询出来的行,获得各个列的值。
cursormovetoFirst itemID  cursorgetLonggetColumnIndexOrThrow);
6.删除数据库中的数据。
// define 'where' part of query. selection " liKE ?"// Specify arguments in placeholder order. selectionArgs valueOfrowID// Issue sql statement.deletetable_name);
7.更新数据库中的数据。
// New value for one column// Which row to update,based on the ID count update.);

如果看本文头昏脑胀的话,只好say sorry ,提供一篇很好的文章更容易理解,链接如下: http://www.jb51.cc/article/p-fcdgiacb-rg.html 总结

以上是内存溢出为你收集整理的SQLite 数据库 *** 作全部内容,希望文章能够帮你解决SQLite 数据库 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/1173976.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-02
下一篇2022-06-02

发表评论

登录后才能评论

评论列表(0条)

    保存