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;
}
}
brother big brother to help look at this code how to modify ?
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)
------ Solution ------------------------------------ --------
neme? this is the name
------ eference ------------------- --------------------
private void insert ()
{
String sql = "insert into TempTbl (_id, neme) values (1, 'chenxiaowei') ";
getWritableDatabase (). execSQL (sql);
}
table name wrong yo into TempTb1
(· ิ ω · ิ)
------ eference ----------------------- ----------------
yes ah , changed , still the same . . . . . . .
------ eference --------------------------------------- < br>
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, now is the problem , how solution ? Guiqiu . . . . . .
没有评论:
发表评论