0

I'm trying to insert data into the database but it tells me there's no such table. I also have another sqlite database inside the application, i'm not sure if that affects this one, but I don't think so. It uses the same database name, but a different table name. If you think it does affect it, tell me and I'll post up the code for that too. The logcat gives me these messages:

(1) no such table: notes
Error inserting title=Bleh desc=bleh
android.database.sqlite.SQLiteException: no such table: notes (code 1): , while         compiling: INSERT INTO notes(title,desc) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.example.stepsaway.NoteActivity.addEntry(NoteActivity.java:102)
at com.example.stepsaway.NoteActivity.onClick(NoteActivity.java:75)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

The Activity java is:

public class NoteActivity extends Activity implements OnClickListener {

Button buttonLeaveNote; 

private EditText mTitle;
private EditText mDesc;

protected NoteDBHelper noteDB = new NoteDBHelper(NoteActivity.this);

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

    buttonLeaveNote = (Button) findViewById(R.id.buttonLeaveNote);
    buttonLeaveNote.setOnClickListener(this);

    mTitle = (EditText)findViewById(R.id.etitle);
    mDesc = (EditText)findViewById(R.id.edesc);
}

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

@Override
public void onClick(View v) {
    switch(v.getId()){

    case R.id.buttonLeaveNote:

           String title = mTitle.getText().toString();
           String desc = mDesc.getText().toString();


           boolean invalid = false;

           if(title.equals(""))
           {
            invalid = true;
            Toast.makeText(getApplicationContext(), "Please enter a title", Toast.LENGTH_SHORT).show();
           }
           else
               if(desc.equals(""))
               {
                  invalid = true;
                  Toast.makeText(getApplicationContext(), "Please enter description", Toast.LENGTH_SHORT).show();

               } 

               else

                   if(invalid == false)
                   {
                       addEntry(title, desc);
                       Intent i_note = new Intent(NoteActivity.this, JustWanderingActivity.class);
                       startActivity(i_note);
                       //finish();
                      }

                      break;
                      }
                    }

public void onDestroy()
{
    super.onDestroy();
    noteDB.close();
}

private void addEntry(String title, String desc) 
 {
    SQLiteDatabase notedb = noteDB.getWritableDatabase();
    ContentValues values = new ContentValues();
      values.put("title", title);
      values.put("desc", desc);
      //values.put("lati", lati);
      //values.put("lng", lng);
      try
      {
       long newRowId;
       newRowId = notedb.insert(NoteDBHelper.DATABASE_TABLE_NAME, null, values);

       Toast.makeText(getApplicationContext(), "Note successfully added", Toast.LENGTH_SHORT).show();
      }

      catch(Exception e)
      {
       e.printStackTrace();
      } 
}

public void onNothingSelected(AdapterView<?> arg0) {
  // TODO Auto-generated method stub

 }
}

And the Database java is:

public class NoteDBHelper extends SQLiteOpenHelper 

{
 private SQLiteDatabase notedb;

public static final String NOTE_ID = "_nid";
public static final String NOTE_TITLE = "title";
public static final String NOTE_DESC = "desc";
public static final String NOTE_LAT = "lati";
public static final String NOTE_LONG = "lng";


NoteDBHelper noteDB = null;
private static final String DATABASE_NAME = "stepsaway.db";
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_TABLE_NAME = "notes";


private static final String DATABASE_TABLE_CREATE =
        "CREATE TABLE " + DATABASE_TABLE_NAME + "(" +
        "_nid INTEGER PRIMARY KEY AUTOINCREMENT," +
        "title TEXT NOT NULL, desc LONGTEXT NOT NULL);";

public NoteDBHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);
System.out.println("In constructor");
}


@Override
public void onCreate(SQLiteDatabase notedb) {

try{

notedb.execSQL(DATABASE_TABLE_CREATE);


}catch(Exception e){
e.printStackTrace();
}
}



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

}




public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}




public void open() {

getWritableDatabase(); 
}


public Cursor getDetails(String text) throws SQLException 
{

Cursor mCursor =
        notedb.query(true, DATABASE_TABLE_NAME, 
          new String[]{NOTE_ID, NOTE_TITLE, NOTE_DESC}, 
          NOTE_TITLE + "=" + text, 
          null, null, null, null, null);
if (mCursor != null) 
{
    mCursor.moveToFirst();
}
return mCursor;


}
}

Any help would be appreciated, thank you.

Edit: Looks like the problem is creating a second table within the same database name. It won't let me create a second one. The first time creating the DB works fine, but it gives the SQLite Exception with no such table when trying to create another table. Is there some code I need to alter or add to create a second table? Because all i did was create another sqlite database java with the same DATABASE_NAME, but a different DATABASE_TABLE_NAME.

4

3 回答 3

2

我最好的猜测是当你创建nodeDB对象时,活动仍然不可用。因此,您将无法创建表。

您可以尝试将初始化 noteDB 的方式移动到 inside onCreate()

protected NoteDBHelper noteDB;


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

    noteDB = new NoteDBHelper(NoteActivity.this);


    buttonLeaveNote = (Button) findViewById(R.id.buttonLeaveNote);
    buttonLeaveNote.setOnClickListener(this);

    mTitle = (EditText)findViewById(R.id.etitle);
    mDesc = (EditText)findViewById(R.id.edesc);
}
于 2013-10-22T09:23:03.473 回答
1

onUpgrade()是空的,数据库架构版本是2。我猜你的数据库的初始版本没有notes表,后来添加时,空的升级代码无法添加它。但是SQLiteOpenHelper很满意,因为它现在正在运行数据库的第 2 版。

要修复一次,请清理您的应用数据。例如,在设置应用程序中,转到管理应用程序 -> 下载 -> 选择您的应用程序,然后单击“清除应用程序数据”按钮。或者只是卸载并重新安装该应用程序。这种方法在开发过程中已经足够好了。

要为已发布版本修复它,请实施onUpgrade()更新数据库架构并执行任何必要的数据迁移。如果您不担心数据丢失,您可以调用DROP TABLE旧表,然后调用onCreate()以重新创建表。


对于您关于多个表的后续问题:只需使用相同的帮助程序类来管理数据库。每个数据库文件一个助手。在 中创建所有表onCreate()并在onUpgrade().

于 2013-10-22T09:33:18.940 回答
0

关于第一个问题:正如我在评论中解释的那样>

  • 如果您之前创建了数据库并在之后添加了表,这可能会导致问题。表是在第一次创建数据库时创建的。因此,从设备中卸载或清理应用程序中的数据并再次运行。

对于第二个问题:

  • 如果您想创建一个表,请执行您已经为您拥有的表执行的相同操作。

    @覆盖

    公共无效 onCreate(SQLiteDatabase 注意b){

    尝试{

     notedb.execSQL(DATABASE_TABLE_CREATE);
    
     notedb.execSQL(DATABASE_TABLE_CREATE_STRING_2);
    

    }catch(异常 e){ e.printStackTrace(); }

}

于 2013-10-22T10:08:22.773 回答