2012-12-08 52 views
0

我想寫一個Race數據庫,在那裏你會輸入一個比賽名稱,然後點擊那個比賽名稱,並編輯/刪除它。 所以我在我的logcat(錯誤代碼= 1)中出現錯誤。它告訴我沒有創建表,所以我認爲我沒有正確調用我的變量。關於「沒有這樣的表」的Eclipse LogCat錯誤SQLite

//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)); 
} 

}

+2

請從您的設備上卸載應用程序,然後再次啓動您的應用程序。 –

+0

在另一個說明中,使用tablename作爲常量,NOTE_TB =「Note」; – wtsang02

+0

我很困惑。在「onCreate()db.execSQL(」CREATE TABLE Note(_id INT ...「)方法中或在聲明中? –

回答

1

您似乎改變了你的SQL模式在Java代碼中,但沒有告知的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()你的代碼,絕通過增加SCHEMA_VERSION告訴SQLite有一個變化。

0

在您的插入方法中,使第二個值爲空。例如getWritableDatabase().insert("Note",null,cv2);

+0

我仍然遇到logcat錯誤

12-08 14:15:46.240:I/Database (799):sqlite返回:錯誤代碼= 1,msg =沒有這樣的表:注意 12-08 14:15:46.271:E /數據庫(799):錯誤插入注= cb 12-08 14:15:46.271 :E/Database(799):android.database.sqlite.SQLiteException:no such table:note:,while compiling:INSERT INTO note(note)VALUES(?); 12-08 14:15:46.271:E/Database (799):\t at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)

0

我也有同樣的問題yours.I解決it.At首先我改名,我創建表卻無可奈何與CREATE_DATABASE order.Then我注意到它。雖然我糾正了順序,但它不起作用。所以我添加onUpgrade方法,並將DATABASE_VERSION增加到更高的數字,它的工作原理!!!希望它也適用於您。

相關問題