概述[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; import
androID.app.Activity; importandroID.content.ContentValues; import
androID.database.Cursor; import
androID.database.sqlite.sqliteDatabase; import
androID.database.sqlite.sqliteOpenHelper; import
androID.os.Bundle; import
androID.vIEw.VIEw; import
androID.vIEw.VIEw.OnClickListener; import
androID.Widget.button; import
com.wansha.db.DatabaseHelper; public
classActivity12extendsActivity{ private
buttoncreateDatabase; private
buttonupdateDatabase; private
buttoninsert; private
buttonupdate; private
buttonread; private
buttondelete; /**CalleDWhentheactivityisfirstcreated.*/ @OverrIDe voID
onCreate(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()); } class
CreateDatabaseListenerimplementsOnClickListener{ voID
onClick(VIEwv){ sqliteOpenHelpersqlite=newDatabaseHelper(Activity12.this,"sharp"); sqlite.getReadableDatabase(); @H_301_330@
} class
UpdateDatabaseListenerimplementsOnClickListener{ @OverrIDe voID
onClick(VIEwv){ System.out.println("UpdateDatabaseListenerisinvoked!!!"); "sharp",2); class
InsertListener 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"}); class
ReadListener 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); class
DeleteListener 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"> buttonandroID:ID="@+ID/createDatabase" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="创建数据库" />
button
androID:ID="@+ID/updateDatabase" @H_301_330@
androID:layout_height="wrap_content" androID:text="更新数据库" />
button
androID:ID="@+ID/insert" androID:text="插入数据" button
androID:ID="@+ID/update" androID:text="更新数据" button
androID:ID="@+ID/read" androID:text="读取数据" button
androID:ID="@+ID/delete" androID:text="删除数据" </
linearLayout>
db.execsql("create table sharp(ID integer primary key autoincrement,username varchar(255))");
创建自增长ID
DatebaseHelper.java
? package
com.wansha.db; import
androID.content.Context; import
androID.database.sqlite.sqliteDatabase.CursorFactory; import
androID.database.sqlite.sqliteOpenHelper; class
DatabaseHelperextendssqliteOpenHelper{ private
staticfinalintVERSION=1; public
DatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory, int
version){ super
(context,name,factory,version); int
version){ this
(context,version); voID
onCreate(sqliteDatabasedb){ System.out.println("createtableisinvoked!!!"); db.execsql("createtablesharp(IDintegerprimarykeyautoincrement,usernamevarchar(255))"); voID
onUpgrade(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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)