qq_41402692
小白小白小
2020-02-04 13:23
采纳率: 66.7%
浏览 1.0k

为什么运行查询和删除数据库时闪退?

MainActivity

package com.example.administrator.diary;

import android.content.Context;
import android.content.Intent;
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.Button;

public class MainActivity extends AppCompatActivity {
    private Button mm_login,yk_login;
    static final String db_pass="passwordDB";
    static final String tb_pass="passwordTB";
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //调用创建数据库方法
        CREATE();
        //调用按钮监听器方法
        init();
        //游客跳转
        yk_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //为了跳转到日记界面,并实现日记功能
                Intent YD=new Intent(MainActivity.this,DiaryActivity.class);
                startActivity(YD);
            }
        });
    }
    //打开或创建数据库
    public void CREATE(){
        db = openOrCreateDatabase(db_pass, Context.MODE_PRIVATE, null);
        String createTable = " CREATE TABLE IF NOT EXISTS " + tb_pass + "(password_id VARCHAR (4) PRIMARY KEY)";
        db.execSQL(createTable);
    }
    //获取activity_main的控件
    public void init(){
        mm_login=findViewById(R.id.but_mm_login);
        yk_login=findViewById(R.id.but_yk_login);
        //获取数据库数据项
        Cursor cur=db.rawQuery("SELECT * FROM passwordTB",null);
        int amount=cur.getCount();
        //数据库为0则表示没有密码存在数据库,密码跳转到注册密码界面,游客可直接登录日记界面
        if (amount==0){
            mm_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //为了跳转到注册界面,并实现注册功能
                    Intent MR=new Intent(MainActivity.this,RegisterActivity.class);
                    startActivity(MR);
                }
            });
            yk_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //为了跳转到日记界面,并实现日记功能
                    Intent YD=new Intent(MainActivity.this,DiaryActivity.class);
                    startActivity(YD);
                }
            });
        }
        else{//数据项不为0,则有密码已存在
            mm_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //为了跳转到密码输入界面,并实现密码输入功能
                Intent MS=new Intent(MainActivity.this,SendActivity.class);
                startActivity(MS);
            }
        });
            //游客按钮隐藏
            yk_login.setVisibility(Button.INVISIBLE);

        }
    }
}


登录代码

package com.example.administrator.diary;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SendActivity extends AppCompatActivity {
    Button log;
    EditText onesend,twosend;
    static final String db_pass="passwordDB";
    static final String tb_pass="passwordTB";
    SQLiteDatabase db;
    String f_send;
    String s_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        //获取控件地址
        log=findViewById(R.id.btn_log);
        onesend=findViewById(R.id.send);
        twosend=findViewById(R.id.resend);
        //获取密码
        f_send = onesend.getText().toString();
        s_send = twosend.getText().toString();
        //打开或创建数据库
        db=openOrCreateDatabase(db_pass, Context.MODE_PRIVATE,null);
        String createTable=" CREATE TABLE IF NOT EXISTS "+tb_pass+"(password VARCHAR (4))";
        db.execSQL(createTable);
        //调用登录按钮方法
        login();
    }
    //对比输入密码和设置密码是否相同
    private void check(){
        //读取数据库中设置的密码
        Cursor c=db.rawQuery("SELECT password FROM passwordTB",null);
        String p=c.getString(0);
        //判断是否相同
        if (f_send.equals(p)&&s_send.equals(p)){
            Toast.makeText(SendActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            //判断相同后跳转到日记界面
            Intent SD=new Intent(SendActivity.this,DiaryActivity.class);
            startActivity(SD);
        }
        //不同提醒用户
        else
            Toast.makeText(SendActivity.this,"密码输入不对,请重新输入",Toast.LENGTH_SHORT).show();
    }
    //登录按钮功能
    private void login(){
        log.setOnClickListener(new View.OnClickListener() {
            @Override
            //监听器
            public void onClick(View v) {
                //调用检验密码方法
                check();
            }
        });
    }

}

删除数据库代码

package com.example.administrator.diary;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class DiaryActivity extends AppCompatActivity {
    private Button de;
    static final String db_pass="passwordDB";
    static final String tb_pass="passwordTB";
    SQLiteDatabase db;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diary);
        //获取控件位置
        de=findViewById(R.id.delete);
        //打开或创建数据库
        db=openOrCreateDatabase(db_pass, Context.MODE_PRIVATE,null);
        String createTable=" CREATE TABLE IF NOT EXISTS "+tb_pass+"(password VARCHAR (4))";
        db.execSQL(createTable);
        Ondelete();
    }
    //删除密码按钮方法
    private void Ondelete(){
        de.setOnClickListener(new View.OnClickListener() {
            @Override
            //监听器
            public void onClick(View v) {
                //删除数据表方法
                String sql = "delete from passwordTB where id  = 0";
                db.execSQL(sql);
                Toast.makeText(DiaryActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

建表插表代码

        //打开或创建数据库
        db=openOrCreateDatabase(db_pass, Context.MODE_PRIVATE,null);
        String createTable=" CREATE TABLE IF NOT EXISTS "+tb_pass+"(password VARCHAR (4))";
        db.execSQL(createTable);
        compare();
        zc();
    }
    //添加新密码到数据库中
    private void addData(String password){
        ContentValues cv=new ContentValues(1);
        cv.put(" password",password);
        db.insert(tb_pass,null,cv);
    }

闪退后报错

2020-02-04 17:22:49.787 15729-15729/com.example.administrator.diary E/SQLiteLog: (1) no such column: id
2020-02-04 17:22:49.787 15729-15729/com.example.administrator.diary D/AndroidRuntime: Shutting down VM
2020-02-04 17:22:49.788 15729-15729/com.example.administrator.diary E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.administrator.diary, PID: 15729
    android.database.sqlite.SQLiteException: no such column: id (code 1 SQLITE_ERROR): , while compiling: delete from passwordTB where id  = 0
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:985)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:592)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1802)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1730)
        at com.example.administrator.diary.DiaryActivity$1.onClick(DiaryActivity.java:39)
        at android.view.View.performClick(View.java:6913)
        at android.view.View.performClickInternal(View.java:6890)
        at android.view.View.access$3200(View.java:792)
        at android.view.View$PerformClick.run(View.java:27158)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:209)
        at android.app.ActivityThread.main(ActivityThread.java:7021)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872)


程序可以运行,但点击登录按钮或删除按钮后都会返回到mainactivity的界面,再按一次登录或删除就崩溃了,而且还没有达到删除数据库的目的,有大神知道是哪里出问题了吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • as_csdn
    代码的灵魂是bug! 2020-02-04 14:03
    已采纳

    //获取密码
    f_send = onesend.getText().toString();
    s_send = twosend.getText().toString();
    这两句代码不应该放在onCreate方法中,而是应该在点击按钮时执行,也就该放在你的check方法中

    ------------------------------更新线---------------------------------------

    //获取数据库数据项
    Cursor cur=db.rawQuery("SELECT * FROM passwordTB",null);
    int amount=cur.getCount();
    Log.e("amount->",amount+"");
    while(cur.moveToNext()){
        String p = cur.getString(cur.getColumnIndex("password"));
        Log.e("pwd->",p);
    }
    
    点赞 评论
  • qq_46275927
    qq_46275927 2020-02-06 08:09

    手机的文件代码出现了问题

    点赞 评论

相关推荐