首页 > 解决方案 > 没有在我的数据库(SQLite)中创建表,Toast 显示成功

问题描述

我试图让这个应用程序通过 SQLiteDATABASE 向数据库添加值并最终显示它们,但是每当我运行这个应用程序并添加一些数据时,我都会为成功干杯,但是当我通过工作室签入设备管理器时我得到这个空的数据库......其中没有创建表......我已经尝试了很多来完成这项工作,我认为这是因为没有调用 onCreate 方法......所以我尝试删除以前的数据库并重新开始..但我仍然得到空数据库... logcat 看起来也不错...

请帮帮我 ...

这是我的 DBHelper 类

package com.example.ankur.login;

import android.content.ContentValues;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter extends SQLiteOpenHelper {

public static final String DB_name="seminar2.db";
public static final String TABLE_name="seminar";
public static final String col1="Date";
public static final String col2="Department";
public static final String col3="Topic";
public static final String col4="Speaker";
public static final String col5="No_of_Students";
public static final String col6="Guests";
public static final String col7="Organisation";
public DBAdapter(Context context) {
    super(context, DB_name, null, 1);
    SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query ="create table "+TABLE_name+" (SEM_ID INTEGER PRIMARY KEY AUTOINCREMENT, "+col1+" date, "+col2+" varchar(15), "+col3+" varchar(15), "+col4+" varchar(15), "+col5+" NUMBER(100), "+col6+" varchar(15), "+col7+ " varchar(15) "+" );";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_name);
    onCreate(db);
}

public boolean insetDATA(String date,String dept,String topic,String fkin_guests,String orgy,String speakr,int no){

    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(col1,date);
    cv.put(col2,dept);
    cv.put(col3,topic);
    cv.put(col4,speakr);
    cv.put(col5,no);
    cv.put(col6,fkin_guests);
    cv.put(col7,orgy);

    long result=db.insert(TABLE_name,null,cv);
    if(result==-1)
        return false;
    else
        return true;
}
}

这是我的活动添加

package com.example.ankur.login;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ADD extends AppCompatActivity{    
DBAdapter myDB;
EditText date,topic,dept,speaker,guests,org,students;
Button addy;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    myDB=new DBAdapter(this);


    date=findViewById(R.id.ID_date);
    topic=findViewById(R.id.ID_topic);
    dept=findViewById(R.id.ID_Dept);
    speaker=findViewById(R.id.ID_Speaker);
    guests=findViewById(R.id.ID_Guests);
    org=findViewById(R.id.ID_Org);
    students=findViewById(R.id.ID_No);
    addy=findViewById(R.id.BUTTON_Add);
    add();
}
public void add()
{
    addy.setOnClickListener(
            new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    boolean isInserted=myDB.insetDATA(date.getText().toString(),dept.getText().toString(),topic.getText().toString(),guests.getText().toString(),org.getText().toString(),speaker.getText().toString(),Integer.parseInt(students.getText().toString()));
                    if(isInserted)
                        Toast.makeText(ADD.this,"Success",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(ADD.this,"FAIL!!",Toast.LENGTH_LONG).show();
                }
            }
    );
}
}

标签: androidandroid-sqlite

解决方案


我相信您的问题在于您如何/使用什么来查看数据库。我已经复制了你的代码并运行它,它工作正常。

我会建议一些小的补充,让你看看你的代码是否正常。

首先是DBAdapter类中的一个新方法,这会将所有行提取到 Cursor 中并返回 Cursor :-

public Cursor getAll() {
    return this.getWritableDatabase().query(TABLE_name,null,null,null,null,null,null);
}

然后对 ADD Activity 中的add方法进行一些更改以调用上述方法并使用DatabaseUtils dumpCursor方法:-

public void add() {
    addy.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    boolean isInserted=myDB.insetDATA(date.getText().toString(),dept.getText().toString(),topic.getText().toString(),guests.getText().toString(),org.getText().toString(),speaker.getText().toString(),Integer.parseInt(students.getText().toString()));
                    if(isInserted) {
                        Toast.makeText(ADD.this, "Success", Toast.LENGTH_LONG).show();
                        Log.d("ADDRESULT", "Added row"); //<<<<<<<<<< Optional
                        Cursor csr = myDB.getAll();  //<<<<<<<<<< Get The Cursor
                        DatabaseUtils.dumpCursor(csr); //<<<<<<<<<< Dump the cursor (to the log)
                        csr.close(); //<<<<<<<<< Should always close a Cursor when done with it
                    } else {
                        Toast.makeText(ADD.this, "FAIL!!", Toast.LENGTH_LONG).show();
                    }
                }
    }
    );
}

使用您的代码副本并在两次运行应用程序后(每次运行添加 1 行),然后日志中的结果是:-

09-30 08:25:57.238 1933-1933/so52573525.so52573525 D/ADDRESULT: Added row
09-30 08:25:57.238 1933-1933/so52573525.so52573525 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@456d67
    0 {
       SEM_ID=1
       Date=2018-09-01
       Department=Area 1
       Topic=Blah
       Speaker=Fred
       No_of_Students=10
       Guests=Tom
       Organisation=The org
    }
    1 {
       SEM_ID=2
       Date=2018-08-01
       Department=Area 2
       Topic=Another
       Speaker=Mary
       No_of_Students=100
       Guests=Sue, Tom and Anne
       Organisation=Acme
    }
    <<<<<

从设备资源管理器获取数据库:-

在此处输入图像描述

  • 请注意,当我使用现有项目回答问题时,存在其他数据库。

右键保存:-

在此处输入图像描述

在工具中打开它(用于 SQlite 的数据库浏览器):-

在此处输入图像描述

结构 :-

在此处输入图像描述

  • 表 android_metadata 由 SQliteDatabase 方法创建,它包含用于区域设置的一行。
  • 表 sqlite_sequence 是在使用 AUTOINCREMENT 关键字时创建的,它存储最高分配的rowid

研讨会表中的数据:-

在此处输入图像描述


推荐阅读