0

我正在尝试编写一个种族数据库,您可以在其中输入种族名称,然后单击该种族名称,然后编辑/删除它。所以我的 logcat 中出现错误(错误代码=1)。它告诉我没有创建表,所以我认为我没有正确调用我的变量。

//LogCat
12-08 12:46:21.019: I/INFORMATION(650): You entered the insert method
12-08 12:46:21.019: I/Database(650): sqlite returned: error code = 1, msg = no such table: Note
12-08 12:46:21.062: E/Database(650): Error inserting note=ft
12-08 12:46:21.062: E/Database(650): android.database.sqlite.SQLiteException: no such table:     Note: , while compiling: INSERT INTO Note(note) VALUES(?);
12-08 12:46:21.062: E/Database(650):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-08 12:46:21.062: E/Database(650):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
12-08 12:46:21.062: E/Database(650):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)

//View races java code
package com.CIS2818.tritracker;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class View_races extends Activity {

NoteAdapter adapter=null;
RaceHelper helper2=null;
Cursor dataset_cursor=null;
EditText editNote2=null;
String noteId2=null;

String TAG = "INFORMATION";

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try
    {
        setContentView(R.layout.activity_view_races);

        ListView list2=(ListView)findViewById(R.id.list2);
        editNote2 = (EditText)findViewById(R.id.myEditText2);

        helper2=new RaceHelper(this);

        dataset_cursor=helper2.getAll();

        startManagingCursor(dataset_cursor);

        adapter=new NoteAdapter(dataset_cursor);

        list2.setAdapter(adapter);


        Button btnSimple2 = (Button) findViewById(R.id.btnSimple2);

        btnSimple2.setOnClickListener(onSave);


        Button btnDelete2 = (Button) findViewById(R.id.btnDelete2);

        btnDelete2.setOnClickListener(onDelete);


        list2.setOnItemClickListener(onListClick);


    }
    catch (Exception e)
    {

        Log.e("ERROR", "ERROR IN CODE: " + e.toString());


        e.printStackTrace();
    } 
}
@Override
public void onDestroy() {
    super.onDestroy();
    helper2.close();
}

private View.OnClickListener onSave=new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v) {
        Log.i(TAG,"You passed through the save method");
        if (noteId2==null) {
            helper2.insert(editNote2.getText().toString());
        }
        else{
            helper2.update(noteId2, editNote2.getText().toString());

            noteId2=null;

        }
        dataset_cursor.requery();

        editNote2.setText("");
    }
};    


private View.OnClickListener onDelete=new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v) {

        if (noteId2==null) {
            return;
        }
        else{

            helper2.delete(noteId2);

            noteId2=null;

        }
        dataset_cursor.requery();

        editNote2.setText("");
    }
};    

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent,
                            View view, int position,
                            long id2) 
    {

        noteId2 =String.valueOf(id2);

        Cursor c=helper2.getById(noteId2);


        c.moveToFirst();

        editNote2.setText(helper2.getNote(c));

    }
};

class NoteAdapter extends CursorAdapter {
    @SuppressWarnings("deprecation")
    NoteAdapter(Cursor c) {
        super(View_races.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt,Cursor c) {
        NoteHolder2 holder=(NoteHolder2)row.getTag();

        holder.populateFrom(c, helper2);
    }

    @Override
    public View newView(Context ctxt, Cursor c,ViewGroup parent) {
        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.row2, parent, false);
        NoteHolder2 holder=new NoteHolder2(row);

        row.setTag(holder);

        return(row);
    }
}

static class NoteHolder2 {
    private TextView noteText2=null;

    NoteHolder2(View row) {
        noteText2=(TextView)row.findViewById(R.id.note2);
    }

    void populateFrom(Cursor c, RaceHelper helper) {
        noteText2.setText(helper.getNote(c));

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_view_races, menu);
    return true;
}
//Button Method to return to the main Menu
public void Menu(View v){
    Intent intent = new Intent(this, MainMenuActivity.class);
    startActivity(intent);
}
//Button Method to go to the Race Activity
public void Races(View v){
    Intent intent = new Intent(this, UpComingRaceActivity.class);
    startActivity(intent);
}}      

//RaceHelper java code
package com.CIS2818.tritracker;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

class RaceHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="note2.db";
private static final int SCHEMA_VERSION=1;
String TAG ="INFORMATION";
public RaceHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE Note (_id INTEGER PRIMARY KEY AUTOINCREMENT, note TEXT);");
}

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

}
public void insert(String note2) {
    Log.i(TAG,"You entered the insert method");
    ContentValues cv2=new ContentValues();
    cv2.put("note", note2);

    getWritableDatabase().insert("Note", "note", cv2);

}
public void update(String id, String note2) {
    ContentValues cv2=new ContentValues();
    String[] args={id};

    cv2.put("note", note2);

    getWritableDatabase().update("Note", cv2, "_id=?", args);

}
public void delete(String id2) {

    getWritableDatabase().delete("Note", "_id=?", new String[] {id2});
}



public Cursor getAll() {
    return(getReadableDatabase()
                    .rawQuery("SELECT _id, note FROM Notes",
                                        null));
}


public String getNote(Cursor c2) {
    return(c2.getString(1));
}

public Cursor getById(String id2) {
    String[] args={id2};

    return(getReadableDatabase()
                    .rawQuery("SELECT _id, note FROM Note WHERE _id=?",
                                        args));
}

}

4

3 回答 3

1

看来您已经在 J​​ava 代码中更改了 SQL 架构,但尚未通知 SQLite。为了让您的 SQLiteOpenHelper 类使用您在 中提供的新模式onCreate(),您需要升级您的数据库。这是最基本的方法。

首先添加一些功能onUpgrade()

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS Note");
    onCreate(db);
} 

现在递增SCHEMA_VERSION以触发升级:

private static final int SCHEMA_VERSION=2;

了解 SQLiteOpenHelper 不会onCreate()为您检查内部代码,必须通过递增来告诉 SQLite 有更改SCHEMA_VERSION

于 2012-12-08T19:00:20.230 回答
0

在您的插入方法中,将第二个值设为空。例如getWritableDatabase().insert("Note",null,cv2);

于 2012-12-08T19:02:08.100 回答
0

我和你有同样的问题。我解决了。起初我重命名了我创建的表,但对 CREATE_DATABASE 订单什么也不做。然后我注意到了。虽然我更正了它不起作用的顺序。所以我添加了 onUpgrade 方法,并将 DATABASE_VERSION 增加到更高的数字,它有效!!!希望它也适用于你。

于 2013-05-02T15:57:47.237 回答