加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用SQLiteDatabase操作SQLite数据库存储数据

发布时间:2020-12-12 19:46:36 所属栏目:百科 来源:网络整理
导读:MainActivity.java package practise.lxm.hello;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.os.Environment;import androi
MainActivity.java
package practise.lxm.hello;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Environment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity {

    SQLiteDatabase db;
    EditText edTxtTitle;
    EditText edTxtContent;
    ListView lvNews;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edTxtTitle = (EditText)findViewById(R.id.edTxt_title);
        edTxtContent = (EditText)findViewById(R.id.edTxt_content);
        lvNews = (ListView)findViewById(R.id.lv_test);
        Button btn = (Button)findViewById(R.id.btn_commit);

        //打开或创建数据库
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            try {
                File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "//test.db3");
                db = SQLiteDatabase.openOrCreateDatabase(file,null);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    insert(db,edTxtTitle.getText().toString(),edTxtContent.getText().toString()); //插入
                }catch (SQLiteException sql){
                    //失败创建表
                    String strSql = "create table news(_id integer primary key autoincrement,title nvarchar(50) not null,content nvarchar(100) not null)";
                    db.execSQL(strSql);
                    //重新插入
                    insert(db,edTxtContent.getText().toString());
                }
                //刷新列表
                String strSql = "select * from news";
                Cursor cursor= db.rawQuery(strSql,null); //查询
                inflateList(cursor);
            }
        });
    }


    //插入信息
    private void insert(SQLiteDatabase db,String title,String content){
        String strInsert = "insert into news(title,content) values(?,?)";
        db.execSQL(strInsert,new String[]{title,content});
    }

    //显示数据到列表
    private void inflateList(Cursor cursor){
        //表中必须含有名为"_id"的列,该列数据类型等无所谓
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.lv_file_item,cursor,new String[]{"title","content"},new int[]{R.id.tv_title,R.id.tv_content},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        lvNews.setAdapter(simpleCursorAdapter);
    }

    protected void onDestroy() {
        if(db != null) { //关闭数据库连接
            db.close();
        }
        super.onDestroy();
    }

}

lv_file_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20sp"
        android:id="@+id/tv_title"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/tv_content"/>
</LinearLayout>

activity_main.xml

<TableLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:padding="5pt">
    <TableRow android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="标题"/>
        <EditText android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edTxt_title"/>
    </TableRow>
    <TableRow android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内容"/>
        <EditText android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edTxt_content"/>
    </TableRow>
    <Button
        android:text="提交"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_commit"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv_test"></ListView>
</TableLayout>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读