Sqlite   发布时间:2022-05-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SQLite数据库存储大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite数据库存储

方法一、命令行模式。在环境变量Path中配置Platform-tools的路径,输入 adb sHell,输入 cd /data/data/cn.bzu.tong.sqlite/databases,输入 sqlite3 BookStore.db,输入 .table查看数据库中的表,输入.scheme,查看建表语句,输入 SELEct * from Book查看表中内容(自己运行显示不出内容)。

方法二、Navicat模式。模拟器运行,点击Create database等按钮,每一次都将模拟器中的BookStore.db导出,在Navicat中新建连接后导入该数据库,打开book表查看表中信息。



一、运行效果

@H_489_26@1、Create databases
@H_489_26@2、Add data 添加数据
@H_489_26@3、update data 更新数据
@H_489_26@4、delete Data 删除数据
@H_489_26@5、Quary data 查询数据

Logcat显示如下图:

@H_489_26@7、模拟器界面MainActivity.xml

二、核心代码

@H_150_87@mainActivity.java

package cn.bzu.tong.sqlite;

import androID.app.Activity;
import androID.content.ContentValues;
import androID.database.cursor;
import androID.database.sqlite.sqliteDatabase;
import androID.os.bundle;
import androID.util.Log;
import androID.vIEw.Menu;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.onClickListener;
import androID.Widget.button;
import cn.bzu.tong.sqList.R;
import cn.bzu.tong.sqlite.db.DBHelper;

public class MainActivity extends Activity {

	private DBHelper dbHelper;
	private button createDatabase,addData,updateData,deleteData,queryData;
    @OverrIDe
    protected voID onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setContentVIEw(R.layout.activity_main);
        dbHelper = new DBHelper(this,"BookStore.db",null,2);
        queryData = (button) findVIEwByID(R.ID.query_data);
        deleteData = (button) findVIEwByID(R.ID.delete_data);
        updateData = (button) findVIEwByID(R.ID.update_data);
        addData = (button) findVIEwByID(R.ID.add_data);
        createDatabase = (button) findVIEwByID(R.ID.creat_databasE);
        createDatabase.setonClickListener(new OnClickListener() {
			
			@OverrIDe
			public voID onClick(View view) {

				dbHelper.getWritableDatabase();
			}
		});
        addData.setonClickListener(new OnClickListener() {
			
			@OverrIDe
			public voID onClick(View view) {

				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",values);//插入第一条数据
				values.clear();
				//开始组装第二条数据
				BACkground-color: rgb(153,"The Lost Symbol");
				BACkground-color: rgb(153,510);
				BACkground-color: rgb(153,19.95);
				BACkground-color: rgb(153,values);//插入第二条数据
			}
		});
        updateData.setonClickListener(new OnClickListener() {
			
			@OverrIDe
			public voID onClick(View view) {

				sqliteDatabase db = dbHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put("price",10.99);
				db.update("Book",values,"name=?",new String[]{"The Da Vinci Code"});
			}
		});
        deleteData.setonClickListener(new OnClickListener() {
			
			@OverrIDe
			public voID onClick(View view) {

				sqliteDatabase db = dbHelper.getWritableDatabase();
				db.delete("Book","pages > ?",new String[]{"400"});//范围
			}
		 queryData.setonClickListener(new OnClickListener() {
			
			@OverrIDe
			public voID onClick(View view) {

				sqliteDatabase db = dbHelper.getWritableDatabase();
				//查询Book表中所有数据
				cursor cursor = db.query("Book",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("MainActivity","book name is "+Name);
						BACkground-color: rgb(255,"book author is "+author);
						BACkground-color: rgb(255,"book page is "+pages);
						BACkground-color: rgb(255,"book price is "+pricE);
						
					}while(cursor.movetoNext());
				}
				cursor.close();
			}
		});
    }


    @OverrIDe
    public Boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    
} 

DBHelper.java

package cn.bzu.tong.sqlite.db;

import androID.content.Context;
import androID.database.sqlite.sqliteDatabase;
import androID.database.sqlite.sqliteDatabase.cursorFactory;
import androID.database.sqlite.sqliteOpenHelper;
import androID.Widget.Toast;

public class DBHelper extends sqliteOpenHelper {

	public static final String create_table_BOOK = "create table book("
			+ "ID INTEGER PRIMary key autoincrement," + "author text,"
			+ "price real," + "pages integer," + "@R_375_4687@t)";
	private Context mContext;

	public static final String create_table_category="create table category(ID INTEGER PRIMary key autoincrement," +
			"category_@R_375_4687@t,category_code Integer)";
	public DBHelper(Context context,String name,cursorFactory factory,int version) {
		super(context,name,factory,version);
		mContext = context;
	}

	@OverrIDe
	public voID onCreate(sqliteDatabase db) {

		db.execsql(create_table_BOOK);
		db.execsql(create_table_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);
	}

}

activity_main.xml

<linearLayout xmlns:androID="http://scheR_988_11845@as.androID.com/apk/res/androID"
    xmlns:tools="http://scheR_988_11845@as.androID.com/tools"
    androID:layout_wIDth="match_parent"
    androID:layout_height="match_parent"
    tools:context=".MainActivity"
    androID:orIEntation="vertical" >

    <button
        androID:ID="@+ID/creat_database"
        androID:layout_wIDth="match_parent"
        androID:layout_height="wrap_content"
        androID:text="Create database" />
    <button 
        androID:ID="@+ID/add_data"
        androID:layout_wIDth="match_parent"
        androID:layout_height="wrap_content"
        androID:text="Add Data"/>
    <button 
        androID:ID="@+ID/update_data"
        androID:layout_wIDth="match_parent"
        androID:layout_height="wrap_content"
        androID:text="update Data"/>
    <button 
        androID:ID="@+ID/delete_data"
        androID:layout_wIDth="match_parent"
        androID:layout_height="wrap_content"
        androID:text="delete Data"/>
    <button 
        androID:ID="@+ID/query_data"
        androID:layout_wIDth="match_parent"
        androID:layout_height="wrap_content"
        androID:text="query Data"/>
    

</linearLayout>

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:输入.scheme