package com.cxw.sqliteopenhelper;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
public class OpenHelperMainActivity extends Activity {
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_open_helper_main);
MyDBHelper helper = new MyDBHelper (this);
helper.insert ();
helper.query ();
}
class MyDBHelper extends SQLiteOpenHelper {
private static final String CREATE_TABLE_SQL = "create table TempTb1 (_id integer, name text)";
private SQLiteDatabase db;
MyDBHelper (Context c)
{
super (c, "test.db", null, 2);
}
@ Override
public void onCreate (SQLiteDatabase db) {
/ / TODO Auto-generated method stub
db.execSQL (CREATE_TABLE_SQL);
}
@ Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
/ / TODO Auto-generated method stub
}
private void insert ()
{
String sql = "insert into TempTbl (_id, neme) values (1, 'chenxiaowei')";
getWritableDatabase () execSQL (sql);.
}
private void query ()
{
. Cursor c = getWritableDatabase () query ("TempTb1", null, null, null, null, null, null);
if (c.moveToFirst ())
{
for (int i = 0; i
c.move (i);
int id = c.getInt (0);
String name = c.getString (1);
System.out.print (id + ":" + name);
}
}
}
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.activity_open_helper_main, menu);.
return true;
}
}
Big Brother helped brother look how to modify this code?
Remind the following error:
09-20 02:39:15.284: E / AndroidRuntime (1175): at com.cxw.sqliteopenhelper.OpenHelperMainActivity $ MyDBHelper.access $ 0 (OpenHelperMainActivity.java: 39)
09-20 02:39:15.284: E / AndroidRuntime (1175): at com.cxw.sqliteopenhelper.OpenHelperMainActivity.onCreate (OpenHelperMainActivity.java: 18)
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
private void insert ()
{
String sql = "insert into TempTbl (_id, neme) values (1, 'chenxiaowei')";
getWritableDatabase () execSQL (sql);.
}
Table name wrong yo into TempTb1
(・ ิ ω ・ ิ)
Reply:
Yes ah, changed, still the same. . . . . . .
Reply:
neme? This is the name
Reply:
?09-20 06:21:55.300: W / dalvikvm (2302): threadid = 1: thread exiting with uncaught exception (group = 0x40a70930)
09-20 06:21:55.330: E / AndroidRuntime (2302): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.cxw.sqliteopenhelper / com.cxw.sqliteopenhelper.OpenHelperMainActivity}: android.database.CursorIndexOutOfBoundsException: Index 17 requested, with a size of 17
09-20 06:21:55.330: E / AndroidRuntime (2302): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2180)
09-20 06:21:55.330: E / AndroidRuntime (2302): at android.database.AbstractWindowedCursor.getInt (AbstractWindowedCursor.java: 68)
09-20 06:21:55.330: E / AndroidRuntime (2302): at com.cxw.sqliteopenhelper.OpenHelperMainActivity $ MyDBHelper.query (OpenHelperMainActivity.java: 57)
That morning I had to find out, and now the question is, how the solution? Beg. . . . . .
No comments:
Post a Comment