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

时间:2014-04-09 12:41:53

标签: java android sqlite android-sqlite sqliteopenhelper

我的应用中有一个ListView,用户可以添加/删除项目。我在使用SQLiteDatabase时使用SQLiteDatabase来存储不使用应用的项目,但是"No such table found:subjects(code 1)给了我一个奇怪的错误SQLiteDatabase。我无法弄清楚为什么会发生这种情况,我之前已经使用了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(); } } 几次,但这次它只是不起作用。

这是我的代码:

SubjectsDatabase.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;
}
}

Subject.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;
}
}

Add_Notes.java -

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

以下是错误日志 -

{{1}}

请帮我弄清楚我的代码有什么问题。谢谢Adavnce !!

3 个答案:

答案 0 :(得分:7)

这是一个很容易解决的问题:你错过了一个空间(还有一个额外的UNDERSCORE):

"CREATE_TABLE" + TABLE_SUBJECTS + "("

和这里(还有额外的UNDERSCOREs)

SUBJECT_ID + "INTEGER_PRIMARY_KEY, "

在这里

SUBJECT_NAME + "TEXT" 

应该是:

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

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

SUBJECT_NAME + " TEXT" 

由于错误的CREATE TABLE语法,因此未创建表。

答案 1 :(得分:0)

将此行更改为

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

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

你需要在columname和datatype之后添加空格。然后卸载以前的应用并再次运行

答案 2 :(得分:0)

为什么不从Eclipse的 DDMS 角度检查您的SQLite数据库 - &gt;文件管理器。下载到您的桌面并使用任何 SQLite浏览器工具进行检查。

如果您发现表格丢失,则需要在创建表格时添加 try - catch 块(以及其他所有位置作为推荐),并记录异常,以便您知道现场背后发生了什么。 db.execSQL抛出android.database.SQLException;,因此你可以抓住它。

此外,load_library(linker.cpp:759): library "libmaliinstr.so" not found错误与缺少权限有关:RECORD_AUDIO&amp; WRITE_EXTERNAL_STORAGE。需要将权限添加到AndroidManifest.xml文件中。

注意:如果您在代码中使用日志多个例外,请不要忘记在发布应用时禁用日志记录。