我试图使该应用程序通过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();
}
}
);
}
}
答案 0 :(得分:0)
我可以建议您使用Room数据库而不是SQLite。我不知道是否需要使用sql,所以也许这没用,但是使用Room之后,我认为没有人应该回到SQLite时代。它还处理重要的sql安全性和缓存问题。
设置非常简单:
Seminar.java
@Entity
public class Seminar {
@PrimaryKey
private int uid;
@ColumnInfo(name = "date")
private String date;
@ColumnInfo(name = "department")
private String department;
@ColumnInfo(name = "topic")
private String topic;
... etc
// Getters and setters are ignored for brevity,
// but they're required for Room to work.
}
SeminarDao.java
@Dao
public interface SeminarDao {
@Query("SELECT * FROM seminar")
List<Seminar> getAll();
@Insert
void insert((Seminar seminar);
@Insert
void insertAll((Seminar... seminars);
@Delete
void delete(Seminar seminar);
}
SeminarDatabase.java
@Database(entities = {Seminar.class}, version = 1)
public abstract class SeminarDatabase extends RoomDatabase {
public abstract SeminarDao seminarDao();
private static volatile SeminarDatabase INSTANCE;
static SeminarDatabase getDatabase(final Context context) {
if (INSTANCE == null) {
synchronized (SeminarDatabase.class) {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
SeminarDatabase.class, "seminar")
.build();
}
}
}
return INSTANCE;
}
}
然后您可以使用上面的单例模式获取实例。
您可以使用asynctask作为Room插入这样的值,以确保您不能在ui线程中执行任何长时间运行的操作:
mSeminarDao = db.seminarDao();
public void insert (Seminar seminar) {
new insertAsyncTask(mSeminarDao).execute(seminar);
}
private static class insertAsyncTask extends AsyncTask<Seminar Void, Void> {
private SeminarDao mAsyncTaskDao;
insertAsyncTask(SeminarDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(final Seminar... params) {
mAsyncTaskDao.insert(params[0]);
return null;
}
}
只需将其添加到您应用的build.gradle
中即可:
dependencies {
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
}
我从Room docs here获得了所有这些示例。
答案 1 :(得分:0)
我认为您的问题在于查看数据库的方式/方式。我已经复制了您的代码并运行它,并且效果很好。
我建议您添加一些较小的内容,以便您查看代码是否正确。
首先在 DBAdapter 类中添加一个新方法,这会将所有行提取到Cursor中并返回Cursor:-
public Cursor getAll() {
return this.getWritableDatabase().query(TABLE_name,null,null,null,null,null,null);
}
然后对ADD活动中的 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的数据库浏览器)中打开它:-
结构:-
“研讨会”表中的数据:-