sqlite错误打开跟踪文件

时间:2013-03-15 17:44:23

标签: android sqlite

当涉及到android编程时,我是一个菜鸟,我特别难以理解SQLite。当我运行我的应用程序时它加载正常但出现错误“错误打开跟踪文件:没有这样的文件或目录(2)”。然后,当我点击我的按钮,它应该将字符串awesome插入我的数据库,然后在textView中返回它们,这些错误出现,应用程序崩溃:

03-15 17:35:01.523: E/Trace(794): error opening trace file: No such file or directory (2)
03-15 17:35:02.223: D/gralloc_goldfish(794): Emulator without GPU emulation detected.
03-15 17:37:48.623: E/SQLiteLog(794): (1) no such column: _id
03-15 17:37:48.623: D/AndroidRuntime(794): Shutting down VM
03-15 17:37:48.623: W/dalvikvm(794): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-15 17:37:48.673: E/AndroidRuntime(794): FATAL EXCEPTION: main
03-15 17:37:48.673: E/AndroidRuntime(794): java.lang.IllegalStateException: Could not execute method of the activity
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.view.View$1.onClick(View.java:3591)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.view.View.performClick(View.java:4084)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.view.View$PerformClick.run(View.java:16966)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.os.Handler.handleCallback(Handler.java:615)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.os.Looper.loop(Looper.java:137)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.app.ActivityThread.main(ActivityThread.java:4745)
03-15 17:37:48.673: E/AndroidRuntime(794):  at java.lang.reflect.Method.invokeNative(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794):  at java.lang.reflect.Method.invoke(Method.java:511)
03-15 17:37:48.673: E/AndroidRuntime(794):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-15 17:37:48.673: E/AndroidRuntime(794):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-15 17:37:48.673: E/AndroidRuntime(794):  at dalvik.system.NativeStart.main(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794): Caused by: java.lang.reflect.InvocationTargetException
03-15 17:37:48.673: E/AndroidRuntime(794):  at java.lang.reflect.Method.invokeNative(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794):  at java.lang.reflect.Method.invoke(Method.java:511)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.view.View$1.onClick(View.java:3586)
03-15 17:37:48.673: E/AndroidRuntime(794):  ... 11 more
03-15 17:37:48.673: E/AndroidRuntime(794): Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, content FROM exampletable WHERE _id = 4
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
03-15 17:37:48.673: E/AndroidRuntime(794):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
03-15 17:37:48.673: E/AndroidRuntime(794):  at com.example.sqlite.MainActivity.insertButton(MainActivity.java:38)
03-15 17:37:48.673: E/AndroidRuntime(794):  ... 14 more

我的MainActivity.java:

package com.example.sqlite;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    DBHelper dbhelper = new DBHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void insertButton(View view){
        SQLiteDatabase db = dbhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("content", "Awesome");

        long newRowId = db.insert("exampletable", null, values);

        String[] projection = {"_id", "content"};

        Cursor c = db.query("exampletable", projection, "_id = " + newRowId, null, null, null, null, null);

        c.moveToFirst();

        String content = c.getString(c.getColumnIndex("content"));

        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setText(content);

    }

}

我的DBHelper.java:

package com.example.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "ExampleDB";

    private String ONCREATE_SQL = "CREATE TABLE exampletable(_id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT);";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(ONCREATE_SQL);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

我做错了什么?是因为初始错误点击按钮时是错误还是分开?非常感谢。

1 个答案:

答案 0 :(得分:0)

SQL语句不能是executet,因为数据库中没有列“_id”。 你试过重新创建表吗?

确保正确创建表:

只需增加DATABASE_VERSION并实施onUpgrade方法:

@Override
public void onUpgrade(SQLiteDatabase database) {
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(database);
}

然后在创建数据库表时检查日志是否有任何错误。 如果没有,请查看/data/data/packename以找到您的SQLite数据库;如果表已成功创建,则pull在DDMS透视图中check