SQLite Database

Posted lyszyl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLite Database相关的知识,希望对你有一定的参考价值。

布局文件

技术图片

<?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.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,null,null,null,null,null,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,null,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",name);
        contentValues.put("age",age);
        db.update(MySQLHelper.PersonTable,contentValues,"name=?",new String[]{name});
        reload();
    }

    public void retrieve(View view){
        String name = editText_name.getText().toString();
        Cursor cursor=db.query(MySQLHelper.PersonTable,null,"name like *",new String[]{name},null,null,null,null);
        adapter.swapCursor(cursor);
    }


    private void reload() {
        Cursor cursor = db.query(MySQLHelper.PersonTable,null,null,null,null,null,null,null);
        adapter.swapCursor(cursor);
    }
}

以上是关于SQLite Database的主要内容,如果未能解决你的问题,请参考以下文章

android.database.sqlite.SQLiteException:没有这样的表:国家(代码 1 SQLITE_ERROR)

android.database.sqlite.SQLiteException:靠近“编码”:语法错误(代码 1)

android.database.sqlite.SQLiteException:在“Foreign”附近:语法错误(代码 1):

插入 android.database.sqlite.SQLiteConstraintException 时出错:NOT NULL 约束失败:result.high(代码 1299)

Android Database(SQLite)参数绑定问题初探

java.lang.RuntimeException:android.database.sqlite.SQLiteException:没有这样的表:media_store_extension(代码1)