0

我是 Android 开发者的初学者。我想开发连接到 SQL-LITE 的应用程序。但我无法解决问题。我也在这篇文章中附上了我的 Logcat。如果可能,请发布您的代码和解决问题的解决方案,因为我是初学者。对不起我的英语不好。这些是我的代码。

类 SimpleNoteActivity

import java.util.Date;    
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static com.example.sudoku.Constants.*;
import static android.provider.BaseColumns._ID;

public class SimpleNoteActivity extends Activity{
    private NotesHelper helper;
    private static String[] COLUMNS= {_ID, TIME, CONTENT};
    //private static String[] COLUMNS= {_ID, CONTENT};
    private static String ORDER_BY = TIME+" DESC";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note); 
        helper = new NotesHelper(this);
        try{

            Cursor cursor = getAllNotes();
            showNotes(cursor);

    }
        finally{
            helper.close();
        }


        final EditText txtNewText = (EditText) findViewById(R.id.new_text);
        Button btnSave = (Button) findViewById(R.id.save_button);
        btnSave.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
            try{
                addNote(txtNewText.getText().toString());
                Cursor cursor = getAllNotes();
                showNotes(cursor);
                txtNewText.setText(null);
                }
            finally{
                helper.close();
                }
            }
        });

    }
    private void addNote(String str){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        //values.put(TIME, System.currentTimeMillis());
        values.put(CONTENT, str);
        db.insertOrThrow(TABLE_NAME,null,values);
    }
    private Cursor getAllNotes(){
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, null,null,null,null,ORDER_BY);
        startManagingCursor(cursor);
        return cursor;
    }
    private void showNotes(Cursor cursor){
        StringBuilder builder = new StringBuilder("Message:\n\n");

        while(cursor.moveToNext()){
            long id = cursor.getLong(0);
            long time = cursor.getLong(1);
            String content = cursor.getString(2);
            builder.append("No ").append(id).append(": ");
            String strDate = (String)DateFormat.format("yyy-MMM-dd hh:mm:ss",new Date(time));
            builder.append(strDate).append("\n");
            builder.append("\t").append(content).append("\n");


        }
        TextView tv = (TextView) findViewById(R.id.all_text);
        tv.setText(builder);
    }


}

接口常数

public interface Constants extends BaseColumns{
    public static final String TABLE_NAME = "notes";
    public static final String TIME = "time";
    public static final String CONTENT = "content";
}

笔记助手

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import static com.example.sudoku.Constants.*;
import static android.provider.BaseColumns._ID;
public class NotesHelper extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "simple_note.db";
    private static final int DATABASE_VERSION = 1;

    public NotesHelper(Context context) {
        super(context, DATABASE_NAME, null,DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // TODO Auto-generated method stub 
        db.execSQL("CREATE TABLE "+TABLE_NAME+" ("+_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+TIME+" INTEGER, "+CONTENT+" TEXT NOT NULL);");
        //db.execSQL("CREATE TABLE "+TABLE_NAME+" ("+_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+CONTENT+" TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }


}

日志猫

06-12 16:17:47.795: E/AndroidRuntime(17741): FATAL EXCEPTION: main
06-12 16:17:47.795: E/AndroidRuntime(17741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sudoku/com.example.sudoku.SimpleNoteActivity}: android.database.sqlite.SQLiteException: no such column: time (code 1): , while compiling: SELECT _id, time, content FROM notes ORDER BY time DESC
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.os.Looper.loop(Looper.java:137)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.ActivityThread.main(ActivityThread.java:4867)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at java.lang.reflect.Method.invoke(Method.java:511)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at dalvik.system.NativeStart.main(Native Method)
06-12 16:17:47.795: E/AndroidRuntime(17741): Caused by: android.database.sqlite.SQLiteException: no such column: time (code 1): , while compiling: SELECT _id, time, content FROM notes ORDER BY time DESC
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at com.example.sudoku.SimpleNoteActivity.getAllNotes(SimpleNoteActivity.java:69)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at com.example.sudoku.SimpleNoteActivity.onCreate(SimpleNoteActivity.java:32)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.Activity.performCreate(Activity.java:5047)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-12 16:17:47.795: E/AndroidRuntime(17741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
06-12 16:17:47.795: E/AndroidRuntime(17741):    ... 11 more
06-12 16:17:47.825: E/android.os.Debug(2035): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
4

1 回答 1

0

您注释掉的代码没有时间列。该表很可能已经存在而没有该列。转到应用程序设置和清除数据。

还要通过设置断点确认您的代码实际上正在被调用...或在屏幕上显示一些吐司

于 2013-06-12T09:35:52.283 回答