首页 文章 精选 留言 我的

精选列表

搜索[sqlite3],共393篇文章
优秀的个人博客,低调大师

安卓操作sqlite3,增删改查

创建 layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/create_database" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Create database" /> </LinearLayout> main package demo.jq.com.databasetest; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; /** * @author jim */ public class MainActivity extends AppCompatActivity { /** * 引入数据库助手类 */ private MyDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2); // 初始化创建按钮 Button createDatabase = (Button) findViewById(R.id.create_database); // 设置点击事件 createDatabase.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // 执行写入数据库操作 dbHelper.getWritableDatabase(); } }); } } helper package demo.jq.com.databasetest; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; /** * @author Jim */ public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_BOOK = "create table Book(" + "id integer primary key autoincrement," + "author text," + "price real," // 浮点型 + "pages integer," + "name text)"; // 文本类型 public static final String CREATE_CATEGORY = "create table Category (" + "id integer primary key autoincrement," + "category_name text," + "category_code integer)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) { super(context,name,factory,version); mContext = context; } /** * 数据库创建的时候,才执行 * 如果数据库已经存在,将不会执行这个方法 * @param db */ @Override public void onCreate(SQLiteDatabase db) { // 执行sql语句 db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATEGORY); Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Category"); onCreate(db); } } 添加数据 <Button android:id="@+id/add_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add data" /> // 初始化添加数据按钮 Button addData = (Button) findViewById(R.id.add_data); addData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); // 开始组装第一条数据 values.put("name","The Da Vinci Code"); values.put("author","Dan Brown"); values.put("pages",454); values.put("price",16.96); // 插入第一条数据 db.insert("Book",null,values); // 开始组装第二条数据 values.put("name","The Lost Symbol"); values.put("author","Dan Brown"); values.put("pages",510); values.put("price",19.95); // 插入第二条数据 db.insert("Book",null,values); } }); 修改数据 <Button android:id="@+id/update_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Update data" /> // 更改数据 Button updateData = (Button) findViewById(R.id.update_data); updateData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("price",18.95); // 修改数据 db.update("Book",values,"id = ?",new String[] {"1"}); } }); 删除数据 <Button android:id="@+id/delete_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Delete data" /> // 删除数据 Button deleteData = (Button) findViewById(R.id.delete_data); deleteData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); // 删除数据 db.delete("Book","pages > ?",new String[] {"500"}); } }); 查询数据 <Button android:id="@+id/query_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Query data" /> // 查询数据 Button queryData = (Button) findViewById(R.id.query_data); queryData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); // 查询Book表中所有的数据 Cursor cursor = db.query("Book",null,null,null,null,null,null); if (cursor.moveToFirst()) { do { // 遍历Cursor对象,取出数据 String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int pages = cursor.getInt(cursor.getColumnIndex("pages")); double price = cursor.getDouble(cursor.getColumnIndex("price")); Log.d(TAG,"book name is "+name); Log.d(TAG,"book author is "+author); Log.d(TAG,"book pages is "+pages); Log.d(TAG,"book price is "+price); } while (cursor.moveToNext()); } cursor.close(); } }); 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7698573.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

【转】IOS数据持久化三个方法:plist、sqlite3、Archiver

1、plist 局限性:只有它支持的数据类型可以被序列化,存储到plist中。无法将其他Cocoa对象存储到plist,更不能将自定义对象存储。 支持的数据类型:Array,Dictionary,Boolean,Data,Date,Number和String. xml文件 数据类型截图~其中基本数据(Boolean,Data,Date,Number和String.)、容器 (Array,Dictionary) 写入xml过程:先将基本数据写入容器 再调用容器的 writeToFile 方法,写入。 [theArray writeToFile:filePath atomically:YES]; 拥有此方法的数据类型有: atomically参数,将值设置为 YES。写入文件的时候,将不会直接写入指定路径,而是将数据写入到一个“辅助文件”,写入成功后,再将其复制到指定路径。 2、Archiver 特点:支持复杂的数据对象。包括自定义对象。对自定义对象进行归档处理,对象中的属性需满足:为基本数据类型(int or float or......),或者为实现了NSCoding协议的类的实例。自定义对象的类也需要实现NSCoding。 NSCoding 方法:-(id)initWithCoder:(NSCoder *)decoder; - (void)encodeWithCoder:(NSCoder *)encoder; 参数分别理解为解码者和编码者。 例如创建自定义类Student:NSObject <NSCoding> 1 #import " Student.h " 2 3 4 @implementation Student 5 @synthesize studentID; 6 @synthesize studentName; 7 @synthesize age; 8 @synthesize count; 9 10 - ( void )encodeWithCoder:(NSCoder * )encoder 11 { 12 [encoder encodeObject: studentID forKey: kStudentId]; 13 [encoder encodeObject: studentName forKey: kStudentName]; 14 [encoder encodeObject: age forKey: kAge]; 15 [encoder encodeInt:count forKey:kCount]; 17 } 18 19 - (id)initWithCoder:(NSCoder * )decoder 20 { 21 if (self == [super init]) { 22 self.studentID = [decoder decodeObjectForKey:kStudentId]; 23 self.studentName = [decoder decodeObjectForKey:kStudentName]; 24 self.age = [decoder decodeObjectForKey:kAge]; 25 self.count = [decoder decodeIntForKey:kCount]; 27 } 28 return self; 29 } 30 31 @end 编码过程: 1 /* encoding */ 2 Student * theStudent = [[Student alloc] init]; 3 theStudent.studentID = @" 神马 " ; 4 theStudent.studentName = @" shenma " ; 5 theStudent.age = @" 12 " ; 6 7 NSMutableData * data = [[NSMutableData alloc] init]; 8 NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; 9 10 [archiver encodeObject: theStudent forKey: @" student " ]; NSKeyedArchiver可以看作“加密器”,将student实例编码后存储到data NSMutableData 可看作“容器”,并由它来完成写入文件操作(inherits NSData)。 解码过程: 1 /* unencoding */ 2 Student * studento = [[Student alloc] init]; 3 data = [[NSData alloc] initWithContentsOfFile:documentsPath]; 4 NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; 5 6 studento = [unarchiver decodeObjectForKey: @" student " ]; 7 [unarchiver finishDecoding]; 根据键值key得到反序列化后的实例。 3、SQLite 本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/archive/2012/03/30/2424944.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册