簡體   English   中英

在sqllite android中工作時出錯

[英]error from working in sqllite android

例如,我想從活動B中獲取一些數據,然后將它們放到數據庫中,然后保存它們。畢竟,從活動A中使用此數據創建了listView,但是出現錯誤。

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
    Button btnadd;
    LinearLayout llMain;
Time today = new Time(Time.getCurrentTimezone());
DBAdapter myDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        btnadd = (Button) findViewById(R.id.button);
        //llMain = (LinearLayout) findViewById(R.id.llMain);
        btnadd.setOnClickListener(this);
        openDB();
        populateListView();
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent(this, result.class);
                startActivityForResult(intent, 1);
                break;

            }
        }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        String name = data.getStringExtra("name");
        String group = data.getStringExtra("group");
today.setToNow();
    String timestamp  = today.format("%Y-%m-%d %H:%M:%S");
myDb.insertRow(name,timestamp,group);
populateListView();





            }

private void openDB(){
    myDb = new DBAdapter(this);
myDb.open();
}
private void populateListView(){
    Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_ROWID,DBAdapter.KEY_TASK};
    int[] toViewIDs = new int[]{R.id.textView3,R.id.textView6};
    SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.item_layout,cursor,fromFieldNames,toViewIDs,0);
    ListView myList = (ListView) findViewById(R.id.listView);
    myList.setAdapter(myCursorAdapter);
}

    }

ResultActivity:

public class result extends ActionBarActivity implements View.OnClickListener {
    EditText etName;
    Button btnOk;
    EditText etData;
    boolean bIcon = true;
ImageButton star;
    String group;
    Intent intent = new Intent();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        etName = (EditText) findViewById(R.id.editText);
        btnOk = (Button) findViewById(R.id.button2);
star = (ImageButton) findViewById(R.id.imageButton);
        btnOk.setOnClickListener(this);
        star.setOnClickListener(this);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:

            intent.putExtra("task", etName.getText().toString());

            setResult(RESULT_OK, intent);
            finish();
                break;
            case R.id.imageButton:
                if (bIcon) {
                    star.setImageResource(R.drawable.star1);
                    group = "top";

                }
                else

                    star.setImageResource(R.drawable.star);
                group = null;
                intent.putExtra("group",group);
                bIcon = !bIcon;
                break;
        }
    }

DBADAPTER:

public class DBAdapter {

    private static final String TAG = "DBAdapter"; //used for logging database version changes

    // Field Names:
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TASK = "task";
    public static final String KEY_DATE = "date";
    public static final String KEY_GROUP = "group";

    public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_TASK, KEY_DATE,KEY_GROUP};

    // Column Numbers for each Field Name:
    public static final int COL_ROWID = 0;
    public static final int COL_TASK = 1;
    public static final int COL_DATE = 2;
    public static final int COL_GROUP = 3;

    // DataBase info:
    public static final String DATABASE_NAME = "dbToDo";
    public static final String DATABASE_TABLE = "mainToDo";
    public static final int DATABASE_VERSION = 2; // The version number must be incremented each time a change to DB structure occurs.

    //SQL statement to create database
    private static final String DATABASE_CREATE_SQL = 
            "CREATE TABLE " + DATABASE_TABLE 
            + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_TASK + " TEXT NOT NULL, " +KEY_GROUP + " TEXT, "
            + KEY_DATE + " TEXT"
            + ");";

    private final Context context;
    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;


    public DBAdapter(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }

    // Open the database connection.
    public DBAdapter open() {
        db = myDBHelper.getWritableDatabase();
        return this;
    }

    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }

    // Add a new set of values to be inserted into the database.
    public long insertRow(String task, String date,String group) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TASK, task);
        initialValues.put(KEY_DATE, date);
        initialValues.put(KEY_GROUP, group);

        // Insert the data into the database.
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    // Delete a row from the database, by rowId (primary key)
    public boolean deleteRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        return db.delete(DATABASE_TABLE, where, null) != 0;
    }

    public void deleteAll() {
        Cursor c = getAllRows();
        long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
        if (c.moveToFirst()) {
            do {
                deleteRow(c.getLong((int) rowId));              
            } while (c.moveToNext());
        }
        c.close();
    }

    // Return all data in the database.
    public Cursor getAllRows() {
        String where = null;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Get a specific row (by rowId)
    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Change an existing row to be equal to new data.
    public boolean updateRow(long rowId, String task, String date,String group) {
        String where = KEY_ROWID + "=" + rowId;
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_TASK, task);
        newValues.put(KEY_DATE, date);
        newValues.put(KEY_GROUP, group);
        // Insert it into the database.
        return db.update(DATABASE_TABLE, newValues, where, null) != 0;
    }


    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE_SQL);           
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data!");

            // Destroy old database:
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

            // Recreate new database:
            onCreate(_db);
        }
    }


}

錯誤:

06-15 15:02:34.759      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:02:35.710      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:02:36.080      631-660/? E/power﹕ Can not write CPU_PERF_LEVEL_LOCK. (Invalid argument)
06-15 15:02:36.180  23620-23620/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 15:02:36.190     631-1443/? E/Surface﹕ Invalid SurfaceControl
06-15 15:02:36.321  23620-23620/com.example.user.campus E/SQLiteLog﹕ (1) near "group": syntax error
06-15 15:02:36.331      631-659/? E/EmbeddedLogger﹕ App crashed! Process: com.example.user.campus
06-15 15:02:36.331      631-659/? E/EmbeddedLogger﹕ App crashed! Package: com.example.user.campus v1 (1.0)
06-15 15:02:36.331      631-659/? E/EmbeddedLogger﹕ Application Label: Campus
06-15 15:02:36.331  23620-23620/com.example.user.campus E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.campus/com.example.user.campus.MainActivity}: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: CREATE TABLE mainToDo (_id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL, group TEXT, date TEXT);
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: CREATE TABLE mainToDo (_id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL, group TEXT, date TEXT);
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
            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.executeSql(SQLiteDatabase.java:1719)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1650)
            at com.example.user.campus.DBAdapter$DatabaseHelper.onCreate(DBAdapter.java:131)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at com.example.user.campus.DBAdapter.open(DBAdapter.java:53)
            at com.example.user.campus.MainActivity.openDB(MainActivity.java:109)
            at com.example.user.campus.MainActivity.onCreate(MainActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5165)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)
06-15 15:02:38.093      631-660/? E/power﹕ request perf lock level(0) is invalid. (Invalid argument)
06-15 15:02:39.774      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:03:00.116      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:03:04.511      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:03:04.751      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 15:03:17.645      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:03:17.775      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 15:03:18.776      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 9
06-15 15:04:00.120      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:05:56.244      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:06:59.762      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:07:00.192      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 15:07:03.326      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:07:08.701      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:07:09.212      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 15:07:10.924      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:07:11.204      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 102
06-15 15:08:03.760      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:06.052      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:06.303      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 102 to 3
06-15 15:08:06.993      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:07.304      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 15
06-15 15:08:13.260      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:13.320      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 15:08:14.321      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 9
06-15 15:08:49.539      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:50.390      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 3
06-15 15:08:51.020      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:51.391      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 15
06-15 15:08:54.224      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:08:54.394      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 3
06-15 15:08:55.395      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 9
06-15 15:08:58.468      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:09:00.390      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:09:00.400      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 15:09:07.939      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:09:08.419      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 9
06-15 15:10:00.104      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:10:56.364      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:21.771      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:21.972  24191-24191/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 15:11:22.202  24217-24217/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-15 15:11:23.043      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:23.673      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 3
06-15 15:11:24.324      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:24.674      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 3 to 15
06-15 15:11:27.317      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:33.063      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:33.564      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:11:33.694      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 9
06-15 15:12:01.904      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:12:02.745      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 9 to 15
06-15 15:12:09.412      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000
06-15 15:12:09.763      223-271/? E/ThermalDaemon﹕ ril.data.radio_tech prop: change from 15 to 9
06-15 15:13:00.106      223-265/? E/ThermalDaemon﹕ CPU1 Max freq recover to 1188000

您正在嘗試將名稱group分配給您的其中一列。 group本身是SQLite的關鍵字,因此您不能使用它。

只需將列名從group更改為其他名稱即可。

檢查一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM