
package com.example.androID;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteDatabase.CursorFactory;import androID.database.sqlite.sqliteOpenHelper;public class StuOpenHelper extends sqliteOpenHelper { public StuOpenHelper(Context context) { super(context, "stu.db", null, 1); // Todo auto-generated constructor stub } @OverrIDe public voID onCreate(sqliteDatabase db) { // Todo auto-generated method stub db.execsql("create table stuinfo(_ID integer primary key autoincrement,name varchar(20),age integer)"); } @OverrIDe public voID onUpgrade(sqliteDatabase arg0, int arg1, int arg2) { // Todo auto-generated method stub }}
package com.example.androID;import androID.annotation.Suppresslint;import androID.os.Bundle;import androID.app.Activity;import androID.content.ContentValues;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); } @Suppresslint("WrongConstant") public voID add(VIEw v) { StuOpenHelper helper = new StuOpenHelper(this); sqliteDatabase db = helper.getWritableDatabase(); String name = ((EditText) findVIEwByID(R.ID.et_name)).getText() .toString(); int age = Integer.parseInt(((EditText) findVIEwByID(R.ID.et_age)) .getText().toString()); // 原生sql *** 作, // db.execsql("insert into stuinfo (name,age) values('lisi',22)"); // db.execsql("insert into stuinfo (name,age) values(?,?)",new // Object[]{"kitty",30}); db.execsql("insert into stuinfo (name,age) values(?,?)", new Object[] { name, age }); Toast.makeText(this, "ok", 0).show(); } public voID delete(VIEw vIEw) { StuOpenHelper helper = new StuOpenHelper(this); sqliteDatabase db = helper.getWritableDatabase(); db.execsql("delete from stuinfo where _ID=?", new Object[] { 2 }); Toast.makeText(this, "删除成功", 0).show(); } @Suppresslint("WrongConstant") public voID update(VIEw vIEw) { StuOpenHelper helper = new StuOpenHelper(this); sqliteDatabase db = helper.getWritableDatabase(); db.execsql("update stuinfo set name=? where _ID=?", new Object[] { "micky", 3 }); Toast.makeText(this, "修改成功", 0).show(); } public voID search(VIEw vIEw) { StuOpenHelper helper = new StuOpenHelper(this); sqliteDatabase db = helper.getWritableDatabase(); String s = ""; Cursor cursor = db.rawquery("select * from stuinfo", null); if (cursor.getCount() != 0) { while (cursor.movetoNext()) { s += cursor.getInt(0) + " " + cursor.getString(1) + " " + cursor.getInt(2) + "\n"; } } // Toast.makeText(this, s, 0).show(); ((TextVIEw) (findVIEwByID(R.ID.tv_show))).setText(s); }}
<?xml version="1.0" enCoding="utf-8"?><linearLayout androID:padding="16dp" androID:orIEntation="vertical" androID:background="@drawable/bg" androID:layout_height="match_parent" androID:layout_wIDth="match_parent" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <linearLayout androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:layout_margintop="130dp"> <TextVIEw androID:layout_height="wrap_content" androID:layout_wIDth="wrap_content" androID:textSize="18sp" androID:text="姓 名 :"/> <EditText androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:textSize="16sp" androID:hint="请输入姓名" androID:ID="@+ID/et_name"/> </linearLayout> -<linearLayout androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:layout_marginBottom="10dp"> <TextVIEw androID:layout_height="wrap_content" androID:layout_wIDth="wrap_content" androID:textSize="18sp" androID:text="年 龄:"/> <EditText androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:textSize="16sp" androID:hint="输入年龄" androID:ID="@+ID/et_age"/></linearLayout> -<linearLayout androID:layout_height="wrap_content" androID:layout_wIDth="match_parent"> <button androID:background="#B9B9FF" androID:layout_height="wrap_content" androID:layout_wIDth="0dp" androID:textSize="18sp" androID:text="添加" androID:ID="@+ID/btn_add" androID:onClick="add" androID:layout_weight="1" androID:layout_marginRight="2dp"/> <button androID:background="#DCB5FF" androID:layout_height="wrap_content" androID:layout_wIDth="0dp" androID:textSize="18sp" androID:text="查询" androID:ID="@+ID/btn_query" androID:onClick="search" androID:layout_weight="1" androID:layout_marginRight="2dp"/> <button androID:background="#E6CAFF" androID:layout_height="wrap_content" androID:layout_wIDth="0dp" androID:textSize="18sp" androID:text="修改" androID:ID="@+ID/btn_update" androID:onClick="update" androID:layout_weight="1" androID:layout_marginRight="2dp"/> <button androID:background="#ACD6FF" androID:layout_height="wrap_content" androID:layout_wIDth="0dp" androID:textSize="18sp" androID:text="删除" androID:ID="@+ID/btn_delete" androID:onClick="delete" androID:layout_weight="1"/></linearLayout> -<ScrollVIEw androID:layout_height="wrap_content" androID:layout_wIDth="match_parent"> <TextVIEw androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:layout_margintop="25dp" androID:textSize="20sp" androID:ID="@+ID/tv_show"/></ScrollVIEw></linearLayout>总结
以上是内存溢出为你收集整理的增删改查全部内容,希望文章能够帮你解决增删改查所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)