简体   繁体   English

Android - SQLiteDatabase 没有这样的表错误(代码 1)

[英]Android - SQLiteDatabase no such table error(code 1)

I've a ListView in my app to which the user can Add/Remove items.我的应用程序中有一个ListView ,用户可以向其中添加/删除项目。 I'm using SQLiteDatabase to store the items when app is not in use but, the SQLiteDatabase giving me a strange error saying "No such table found:subjects(code 1) . I can't figure out why's this happening, I've used SQLiteDatabase a few times before, but this time around it's just not working.当应用程序未使用时,我使用SQLiteDatabase来存储项目,但是SQLiteDatabase给了我一个奇怪的错误,说"No such table found:subjects(code 1) 。我不知道为什么会这样,我已经之前使用过SQLiteDatabase几次,但这一次它不起作用。

This is my code:这是我的代码:

SubjectsDatabase.java -主题数据库.java -

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SubjectsDatabase extends SQLiteOpenHelper {

private static final String SUBJECT_ID = "id";
private static final String SUBJECT_NAME = "name";

private static final String DATABASE_NAME = "subjectsDatabase";
private static final String TABLE_SUBJECTS = "subjects";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE = "CREATE_TABLE" + TABLE_SUBJECTS + "(" + 
                        SUBJECT_ID + "INTEGER_PRIMARY_KEY, " + SUBJECT_NAME + "TEXT" + ")";
public SubjectsDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(DATABASE_CREATE);
    }
    catch(SQLException e) {
        e.printStackTrace();
    }
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXSISTS topics");
    onCreate(db);
}
public void addSubject(Subject subject) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues topics = new ContentValues();
    topics.put(SUBJECT_NAME, subject.getSubject());

    db.insert(TABLE_SUBJECTS, null, topics);
    db.close();
}
public void removeSubject(Subject subject) {

    SQLiteDatabase db = this.getWritableDatabase();

    db.delete(TABLE_SUBJECTS, SUBJECT_NAME + " = ?", 
            new String[] {String.valueOf(subject.getSubject())});

    db.close();
}
public ArrayList<Subject> getSubjects(){

    ArrayList<Subject> topics = new ArrayList<Subject>();

    String selectQuery = "SELECT * FROM " + TABLE_SUBJECTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cur = db.rawQuery(selectQuery, null);

    if(cur.moveToFirst()) {
        do {
            Subject subject = new Subject();
            subject.setId(Integer.parseInt(cur.getString(0)));
            subject.setSubject(cur.getString(1));
            topics.add(subject);
        } while(cur.moveToNext());
    }
    db.close();
    return topics;
}
public void updateSubject(Subject old_name, Subject new_name) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues topic = new ContentValues();

    topic.put(SUBJECT_NAME, new_name.getSubject());

    db.update(TABLE_SUBJECTS, topic, SUBJECT_NAME + " = ?", 
            new String[] {String.valueOf(old_name.getSubject())});
    db.close();
}
}

Subject.java -主题.java -

public class Subject {

String _subject;
int _id;
public Subject() {

}
public Subject(String subject) {
    this._subject = subject;
}
public String getSubject() {
    return _subject;
}
public void setSubject(String subject) {
    this._subject = subject;
}
public int getId() {
    return _id;
}
public void setId(int id) {
    this._id = id;
}
}

Add_Notes.java - Add_Notes.java -

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;


public class Add_Notes extends Activity {

private ItemAdapter adapter;
private ArrayList<Subject> menu_items;
private SubjectsDatabase db = new SubjectsDatabase(this);
private ListView subjects;
private InputMethodManager imm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add__notes);

    menu_items = db.getSubjects();
    adapter = new ItemAdapter(this, menu_items);
    subjects = (ListView) findViewById(R.id.subjects);

    subjects.setAdapter(adapter);
}

public void onClickAddSubjects(View v) {
    showDialog(0);
    showKeyboard();
}

protected Dialog onCreateDialog(int id) {
    switch(id) {
    case 0:
        Builder builder = new AlertDialog.Builder(this);
        final EditText SubjectName = new EditText(this);
        SubjectName.setHint("Subject Name");
        builder.setView(SubjectName);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {

                String subjectName = SubjectName.getText().toString();

                db.addSubject(new Subject(subjectName));

                adapter.add(new Subject(subjectName));
                adapter.notifyDataSetChanged();

                SubjectName.setText("");
                dismissDialog(0);
                removeDialog(0);
                hideKeyboard();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {

                SubjectName.setText("");
                dismissDialog(0);
                removeDialog(0);
                hideKeyboard();

            }
        });
        return builder.create();
    }
    return null;
}

public void showKeyboard() {
    imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

public void hideKeyboard() {
    imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_add__notes, menu);
    return true;
}
}

Here are the error logs -这是错误日志 -

04-09 18:08:42.133: E/linker(31508): load_library(linker.cpp:759): library "libmaliinstr.so" not found
04-09 18:08:42.965: E/SQLiteLog(31508): (1) no such table: subjects
04-09 18:08:42.984: E/AndroidRuntime(31508): FATAL EXCEPTION: main
04-09 18:08:42.984: E/AndroidRuntime(31508): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Swap.RR/com.Swap.RR.Add_Notes}: android.database.sqlite.SQLiteException: no such table: subjects (code 1): , while compiling: SELECT * FROM subjects
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.ActivityThread.access$600(ActivityThread.java:174)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.os.Handler.dispatchMessage(Handler.java:107)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.os.Looper.loop(Looper.java:194)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.ActivityThread.main(ActivityThread.java:5409)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at java.lang.reflect.Method.invoke(Method.java:525)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at dalvik.system.NativeStart.main(Native Method)
04-09 18:08:42.984: E/AndroidRuntime(31508): Caused by: android.database.sqlite.SQLiteException: no such table: subjects (code 1): , while compiling: SELECT * FROM subjects
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at com.Swap.RR.SubjectsDatabase.getSubjects(SubjectsDatabase.java:66)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at com.Swap.RR.Add_Notes.onCreate(Add_Notes.java:32)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.Activity.performCreate(Activity.java:5122)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
04-09 18:08:42.984: E/AndroidRuntime(31508):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
04-09 18:08:42.984: E/AndroidRuntime(31508):    ... 11 more

Please, help me figure out what's wrong with my code.请帮我弄清楚我的代码有什么问题。 Thanks in Adavnce!!感谢 Adavnce !!

It's an easy fix: you miss a space here (and have an extra UNDERSCORE):这是一个简单的解决方法:您在这里错过了一个空格(并且有一个额外的下划线):

"CREATE_TABLE" + TABLE_SUBJECTS + "("

and here (and have extra UNDERSCOREs)和这里(并有额外的下划线)

SUBJECT_ID + "INTEGER_PRIMARY_KEY, "

and here和这里

SUBJECT_NAME + "TEXT" 

It should read:它应该是:

"CREATE TABLE " + TABLE_SUBJECTS + " (" // Note: NO UNDERSCORE!

and

SUBJECT_ID + " INTEGER PRIMARY KEY, " // Note: NO UNDERSCORES!

and

SUBJECT_NAME + " TEXT" 

The table isn't created because of the erroneous CREATE TABLE syntax.由于错误的 CREATE TABLE 语法,未创建表。

change this line to将此行更改为

private static final String DATABASE_CREATE = "CREATE_TABLE" + TABLE_SUBJECTS + "(" SUBJECT_ID + "INTEGER_PRIMARY_KEY, " + SUBJECT_NAME + "TEXT" + ")";

to

private static final String DATABASE_CREATE = "CREATE TABLE" + TABLE_SUBJECTS + "(" + 
                    SUBJECT_ID + " INTEGER_PRIMARY_KEY, " + SUBJECT_NAME + " TEXT" + ")";

you need to add space after columname and datatype.您需要在列名和数据类型后添加空格。 Then uninstall your previous app and run it again然后卸载您之前的应用程序并再次运行它

Why don't you check your SQLite db from Eclipse's DDMS perspective -> File Explorer.为什么不从 Eclipse 的DDMS视角 -> 文件资源管理器检查您的 SQLite 数据库。 Download to your desktop and check using any SQLite browser tool .下载到您的桌面并使用任何SQLite 浏览器工具进行检查。

If you find table is missing, you need to add try - catch blocks while creating your table (and everywhere else as recommendation) and log the exception so you know what is happening behind the scene.如果您发现表丢失,您需要在创建表时添加try-catch块(以及其他任何地方作为建议)并记录异常,以便您了解幕后发生的事情。 db.execSQL throws android.database.SQLException; db.execSQL抛出android.database.SQLException; and hence you can catch it.因此你可以抓住它。

Further, load_library(linker.cpp:759): library "libmaliinstr.so" not found error is related to missing permissions: RECORD_AUDIO & WRITE_EXTERNAL_STORAGE.此外, load_library(linker.cpp:759): library "libmaliinstr.so" not found错误与缺少权限有关:RECORD_AUDIO & WRITE_EXTERNAL_STORAGE。 Permissions needs to be added to AndroidManifest.xml file.权限需要添加到 AndroidManifest.xml 文件中。

Note: If you use log several exceptions in your code, don't forget to disable logging while publishing your app.注意:如果您在代码中使用日志记录多个异常,请不要忘记在发布应用程序时禁用日志记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM