SQLite Database

SQLite Database,第1张

概述布局文件 <?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-au

布局文件

<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".MainActivity">    <EditText        androID:ID="@+ID/editText_name"        androID:layout_wIDth="0dp"        androID:layout_height="wrap_content"        androID:layout_marginStart="8dp"        androID:layout_marginleft="8dp"        androID:layout_marginEnd="8dp"        androID:layout_marginRight="8dp"        androID:ems="10"        androID:hint="input name?"        androID:inputType="textPersonname"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="parent" />    <EditText        androID:ID="@+ID/editText_age"        androID:layout_wIDth="0dp"        androID:layout_height="wrap_content"        androID:layout_marginStart="8dp"        androID:layout_marginleft="8dp"        androID:layout_margintop="11dp"        androID:layout_marginEnd="8dp"        androID:layout_marginRight="8dp"        androID:ems="10"        androID:hint="input age?"        androID:inputType="number"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_toBottomOf="@+ID/editText_name" />    <button        androID:ID="@+ID/button"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_margintop="44dp"        androID:layout_marginEnd="10dp"        androID:layout_marginRight="10dp"        androID:onClick="add"        androID:text="Add"        app:layout_constraintEnd_toStartOf="@+ID/button2"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="@+ID/editText_age" />    <button        androID:ID="@+ID/button2"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginEnd="6dp"        androID:layout_marginRight="6dp"        androID:onClick="delete"        androID:text="Delete"        app:layout_constraintBottom_totopOf="@+ID/ListVIEw"        app:layout_constraintEnd_toStartOf="@+ID/button3"        app:layout_constraintStart_toEndOf="@+ID/button" />    <button        androID:ID="@+ID/button3"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginEnd="2dp"        androID:layout_marginRight="2dp"        androID:onClick="update"        androID:text="Update"        app:layout_constraintBottom_totopOf="@+ID/ListVIEw"        app:layout_constraintEnd_toStartOf="@+ID/button4"        app:layout_constraintStart_toEndOf="@+ID/button2" />    <button        androID:ID="@+ID/button4"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginEnd="1dp"        androID:layout_marginRight="1dp"        androID:onClick="retrIEve"        androID:text="RetrIEve"        app:layout_constraintBottom_totopOf="@+ID/ListVIEw"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toEndOf="@+ID/button3" />    <ListVIEw        androID:ID="@+ID/ListVIEw"        androID:layout_wIDth="0dp"        androID:layout_height="0dp"        androID:layout_marginStart="8dp"        androID:layout_marginleft="8dp"        androID:layout_marginEnd="8dp"        androID:layout_marginRight="8dp"        androID:layout_marginBottom="8dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_toBottomOf="@+ID/button" /></androID.support.constraint.ConstraintLayout>

MysqLHelper

package com.example.sqlitedemo2;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;public class MysqLHelper extends sqliteOpenHelper {    public static final String Persontable = "person";    public MysqLHelper(Context context,String name,sqliteDatabase.CursorFactory factory,int version) {        super(context,name,factory,version);    }    @OverrIDe    public voID onCreate(sqliteDatabase db) {        db.execsql("create table if not exists person(_ID integer primary key autoincrement,name text,age integer)");    }    @OverrIDe    public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {    }}

MainActivity.java

package com.example.sqlitedemo2;import androID.content.ContentValues;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.os.AsyncTask;import androID.support.annotation.Nullable;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.CursorAdapter;import androID.Widget.EditText;import androID.Widget.ListVIEw;import androID.Widget.SimpleCursorAdapter;public class MainActivity extends AppCompatActivity {    MysqLHelper MysqLHelper;    sqliteDatabase db;    EditText editText_name,editText_age;    ListVIEw ListVIEw;    SimpleCursorAdapter adapter;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        editText_name = findVIEwByID(R.ID.editText_name);        editText_age = findVIEwByID(R.ID.editText_age);        ListVIEw = findVIEwByID(R.ID.ListVIEw);        MysqLHelper = new MysqLHelper(this,"db",null,1);        db = MysqLHelper.getWritableDatabase();        Cursor cursor = db.query(MysqLHelper.Persontable,null);        adapter = new SimpleCursorAdapter(this,androID.R.layout.simple_List_item_2,cursor,new String[]{"name","age"},new int[]{androID.R.ID.text1,androID.R.ID.text2},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);        ListVIEw.setAdapter(adapter);    }    public voID add(VIEw vIEw){        String name = editText_name.getText().toString();        String age = editText_age.getText().toString();//        db.execsql("insert into person(name,age) values('"+name+"','"+age+"')");        ContentValues contentValues = new ContentValues();        contentValues.put("name",name);        contentValues.put("age",age);        db.insert(MysqLHelper.Persontable,contentValues);        reload();    }    public voID delete(VIEw vIEw){        String name = editText_name.getText().toString();        db.delete(MysqLHelper.Persontable,"name=?",new String[]{name});        reload();    }    public voID update(VIEw vIEw){        String name = editText_name.getText().toString();        String age = editText_age.getText().toString();        ContentValues contentValues = new ContentValues();        contentValues.put("name",age);        db.update(MysqLHelper.Persontable,contentValues,new String[]{name});        reload();    }    public voID retrIEve(VIEw vIEw){        String name = editText_name.getText().toString();        new MyAsyncTask().execute("%"+name+"%");//        Cursor cursor=db.query(MysqLHelper.Persontable,"name like *",new String[]{name},null);//        adapter.swapCursor(cursor);    }    private voID reload() {        Cursor cursor = db.query(MysqLHelper.Persontable,null);        adapter.swapCursor(cursor);    }    class  MyAsyncTask extends AsyncTask<String,Nullable,Cursor>{        @OverrIDe        protected Cursor doInBackground(String... strings) {            Cursor cursor=db.query(MysqLHelper.Persontable,"name like ?",new String[]{"%"+strings[0]+"%"},null);            return cursor;        }        @OverrIDe        protected voID onPostExecute(Cursor cursor) {            super.onPostExecute(cursor);            adapter.swapCursor(cursor);        }    }}
总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存