SQLite的CRUD

SQLite的CRUD,第1张

概述[java]  view plain copy print ? package com.wansha;      import android.app.Activity;   import android.content.ContentValues;   import android.database.Cursor;   import android.database.sqlite.SQLiteD [java] view plain copy print ? packagecom.wansha; importandroID.app.Activity; importandroID.content.ContentValues; importandroID.database.Cursor; importandroID.database.sqlite.sqliteDatabase; importandroID.database.sqlite.sqliteOpenHelper; importandroID.os.Bundle; importandroID.vIEw.VIEw; importandroID.vIEw.VIEw.OnClickListener; importandroID.Widget.button; importcom.wansha.db.DatabaseHelper; publicclassActivity12extendsActivity{ privatebuttoncreateDatabase; privatebuttonupdateDatabase; privatebuttoninsert; privatebuttonupdate; privatebuttonread; privatebuttondelete; /**CalleDWhentheactivityisfirstcreated.*/ @OverrIDe voIDonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); this.createDatabase=(button)this.findVIEwByID(R.ID.createDatabase); this.createDatabase.setonClickListener(newCreateDatabaseListener()); this.updateDatabase=(button)this.findVIEwByID(R.ID.updateDatabase); this.updateDatabase.setonClickListener(newUpdateDatabaseListener()); this.insert=(button)this.findVIEwByID(R.ID.insert); this.insert.setonClickListener(newInsertListener()); this.update=(button)this.findVIEwByID(R.ID.update); this.update.setonClickListener(newUpdateListener()); this.read=(button)this.findVIEwByID(R.ID.read); this.read.setonClickListener(newReadListener()); this.delete=(button)this.findVIEwByID(R.ID.delete); this.delete.setonClickListener(newDeleteListener()); } classCreateDatabaseListenerimplementsOnClickListener{ voIDonClick(VIEwv){ sqliteOpenHelpersqlite=newDatabaseHelper(Activity12.this,"sharp"); sqlite.getReadableDatabase(); @H_301_330@ } classUpdateDatabaseListenerimplementsOnClickListener{ @OverrIDe voIDonClick(VIEwv){ System.out.println("UpdateDatabaseListenerisinvoked!!!"); "sharp",2); classInsertListener sqliteOpenHelpersqlite="sharp"); sqliteDatabasedatabase=sqlite.getWritableDatabase(); ContentValuesvalues=newContentValues(); values.put("username","xiaopeng"); database.insert("sharp",null,values); classUpdateListener"pengpeng"); database.update("sharp",values,"ID=?",153); background-color:inherit; Font-weight:bold">newString[]{"2"}); classReadListener Cursorcursor=database.query("sharp",153); background-color:inherit; Font-weight:bold">newString[]{"ID","username"},153); background-color:inherit; Font-weight:bold">null); while(cursor.movetoNext()){ Stringusername=cursor.getString(cursor.getColumnIndex("username")); System.out.println("username---->"+username); classDeleteListener database.delete("sharp",153); background-color:inherit; Font-weight:bold">newString[]{"1"}); }
main.xml

[HTML] ? <?xmlversion="1.0"enCoding="utf-8"?> <linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical"> button
androID:ID="@+ID/createDatabase" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="创建数据库" /> buttonandroID:ID="@+ID/updateDatabase" @H_301_330@ androID:layout_height="wrap_content" androID:text="更新数据库" /> buttonandroID:ID="@+ID/insert" androID:text="插入数据" buttonandroID:ID="@+ID/update" androID:text="更新数据" buttonandroID:ID="@+ID/read" androID:text="读取数据" buttonandroID:ID="@+ID/delete" androID:text="删除数据" </linearLayout>

db.execsql("create table sharp(ID integer primary key autoincrement,username varchar(255))");

创建自增长ID


DatebaseHelper.java

? packagecom.wansha.db; importandroID.content.Context; importandroID.database.sqlite.sqliteDatabase.CursorFactory; importandroID.database.sqlite.sqliteOpenHelper; classDatabaseHelperextendssqliteOpenHelper{ privatestaticfinalintVERSION=1; publicDatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory, intversion){ super(context,name,factory,version); intversion){ this(context,version); voIDonCreate(sqliteDatabasedb){ System.out.println("createtableisinvoked!!!"); db.execsql("createtablesharp(IDintegerprimarykeyautoincrement,usernamevarchar(255))"); voIDonUpgrade(sqliteDatabasedb,153); background-color:inherit; Font-weight:bold">intoldVersion,153); background-color:inherit; Font-weight:bold">intnewVersion){ System.out.println("updatetableisinvoked!!!"); }


总结

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

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

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

原文地址: http://outofmemory.cn/sjk/1175145.html

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

发表评论

登录后才能评论

评论列表(0条)

保存