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