0

我的程序包含通过 textview 和 editview 进行的简单数据库插入。

在运行有关 sqlite 的一些错误时,logcat 中列出了一些错误。我在下面给出了代码和 logcat。请给我一个解决方案。

FirstActivity.java:

package example.showevent1;

import android.os.Bundle;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class FirstActivity extends FragmentActivity implements OnItemSelectedListener {
    /** Called when the activity is first created. */
    classdbOpenHelper eventsData;
     TextView userSelection;
     Button okButton;
     EditText date;
    private static final String[] items={"Yalahanka","Rajai nagar","Sivaji Nagar","Koramangala","RT Nagar", "Banashankari","Yashwanthpura","Hebbal"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
         okButton = (Button)findViewById(R.id.button2);
         date=(EditText)findViewById(R.id.editText1);
         userSelection=(TextView)findViewById(R.id.textView1);
         Spinner my_spin=(Spinner)findViewById(R.id.spinner1);//data1
        my_spin.setOnItemSelectedListener(this);
        ArrayAdapter aa=new ArrayAdapter(this, android.R.layout.simple_spinner_item,items);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        my_spin.setAdapter(aa);
        okButton.setOnClickListener(new clicker());
        eventsData = new classdbOpenHelper(this);

        }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        userSelection.setText(items[pos]);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
        userSelection.setText("");
    }
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }
    class clicker implements Button.OnClickListener {
        public void onClick(View v) {
            String datevalue = date.getText().toString();
            String Userselectvalue = userSelection.getText().toString();
            SQLiteDatabase  db = eventsData.getWritableDatabase();

            ContentValues cv = new ContentValues();
            //cv.put(classdbOpenHelper.KEY_COUNTED, metersave.getText().toString());
            cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue);
            cv.put(classdbOpenHelper.KEY_DATE,datevalue);
            db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
        }

    }
    @Override
    public void onDestroy() {
      eventsData.close();
    }
}

类 classdbOpenHelper 扩展 SQLiteOpenHelper

package example.showevent1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class classdbOpenHelper extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "_id";
    //public static final String KEY_COUNTED = "countedno";
    public static final String KEY_DESC = "countdesc";
    public static final String KEY_DATE = "countdate";

    public static final String DATABASE_NAME= "countdb";
    public static final String DATABASE_TABLE = "countable";
    public static final int DATABASE_VERSION = 1;


    public classdbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
        //KEY_COUNTED + " INTEGER " +
        KEY_DESC + " TEXT NOT NULL " +
        KEY_DATE + " TEXT NOT NULL);"
                ); 
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
        onCreate(db);

    }

}

日志猫

08-02 10:05:41.211: E/SQLiteLog(990): (1) near "TABLEcountable": syntax error
08-02 10:05:41.270: E/AndroidRuntime(990): FATAL EXCEPTION: main
08-02 10:05:41.270: E/AndroidRuntime(990): android.database.sqlite.SQLiteException: near "TABLEcountable": syntax error (code 1): , while compiling: CREATE TABLEcountable (_id INTEGER PRIMARY KEY AUTOINCREMENT countdesc TEXT NOT NULL countdate TEXT NOT NULL);
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
08-02 10:05:41.270: E/AndroidRuntime(990):  at example.showevent1.classdbOpenHelper.onCreate(classdbOpenHelper.java:28)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
08-02 10:05:41.270: E/AndroidRuntime(990):  at example.showevent1.FirstActivity$clicker.onClick(FirstActivity.java:61)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.view.View.performClick(View.java:4204)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.view.View$PerformClick.run(View.java:17355)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.os.Handler.handleCallback(Handler.java:725)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.os.Looper.loop(Looper.java:137)
08-02 10:05:41.270: E/AndroidRuntime(990):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-02 10:05:41.270: E/AndroidRuntime(990):  at java.lang.reflect.Method.invokeNative(Native Method)
08-02 10:05:41.270: E/AndroidRuntime(990):  at java.lang.reflect.Method.invoke(Method.java:511)
08-02 10:05:41.270: E/AndroidRuntime(990):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-02 10:05:41.270: E/AndroidRuntime(990):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-02 10:05:41.270: E/AndroidRuntime(990):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

我认为

db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +

应该

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +

注意 TABLE 后面的空格。

于 2013-08-02T10:11:25.537 回答