IDEA连接具体的sqlite数据库

IDEA连接具体项目的sqlite数据库:

1、运行代码,后会生成一个数据库文件**.db
在这里插入图片描述

package com.lihua.sqlitetest;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.cursoradapter.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {
    SQLiteDatabase db;
    Button bn = null;
    ListView listView;
    EditText titles,contents;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir()
                .toString() + "/my.db3" , null);

        Log.i("sout","这是指定创建数据库的文件路径"+this.getFilesDir().toString());
        /*
        *this.getFilesDir().toString() 这是指定创建数据库的文件路径
        * my.db3 数据库名
        * */

        listView = (ListView)findViewById(R.id.show);
        titles = (EditText) findViewById(R.id.title);
        contents = (EditText) findViewById(R.id.content);
        bn = (Button)findViewById(R.id.ok);
        bn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View source){
                String title = titles.getText().toString();
                String content = contents.getText().toString();
                try {
                    insertData(db , title , content);
                    Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);
                }
                catch(SQLiteException se){
                    db.execSQL("create table news_inf(_id integer primary key autoincrement," + " news_title varchar(50)," + " news_content varchar(255))");
                    insertData(db , title , content);Cursor cursor = db.rawQuery("select * from news_inf", null);
                    inflateList(cursor);
                }
            }
        });
    }
    private void insertData(SQLiteDatabase db, String title , String content){
        db.execSQL("insert into news_inf values(null , ? , ?)", new String[]{title , content});
    }
    private void inflateList(Cursor cursor){
        SimpleCursorAdapter adapter= new SimpleCursorAdapter(
                MainActivity.this , R.layout.line, cursor
                , new String[]{"news_title" , "news_content"}
                , new int[]{R.id.my_title , R.id.my_content});
        listView.setAdapter(adapter);
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        if (db != null && db.isOpen()){
            db.close();
        }
    }
}

2、根据项目路径和数据库创建的文件路径,找到db的url
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
然后右键
在这里插入图片描述
找到db的文件路径,复制粘贴
在这里插入图片描述

连接sqlite数据库:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
最后点击测试,成功后连接
在这里插入图片描述
连接成功,教程结束
在这里插入图片描述

界面代码,
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

    <EditText
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    <EditText
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:lines="2" />

    <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="插入" />

    <ListView
            android:id="@+id/show"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


</LinearLayout>

line.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal" >

<EditText
        android:id="@+id/my_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120px" />

<EditText
        android:id="@+id/my_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值