两个日志之间的SQLite语句导致我的应用程序崩溃

时间:2016-05-25 17:41:37

标签: android

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
import edittext.com.sqlite4.MainActivity;
import model.MyWish;

public class DatabaseHandler extends SQLiteOpenHelper {
private Context mcontext;

private final ArrayList<MyWish> wishList=new ArrayList<>();
public DatabaseHandler(Context context) {
    super(context, Constants.DATABASE_NAME,null,Constants.DATABASE_VERSION);
    mcontext=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_WISHES_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "("
            + Constants.KEY_ID + " INTEGER PRIMARY KEY, " + Constants.TITLE_NAME +
            " TEXT, " + Constants.CONTENT_NAME + " TEXT, " + Constants.DATE_NAME + " LONG);";

    db.execSQL(CREATE_WISHES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_NAME);

    Log.v("ONUPGRADE", "DROPING THE TABLE AND CREATING A NEW ONE!");

    //create a new one
    onCreate(db);
}

public void addWishes(MyWish wishy) {
    Log.v("ADDING WISH TO DATABASE","ADDING!!!!!!!");

    ContentValues values = new ContentValues();
    values.put(Constants.TITLE_NAME, wishy.getTitle());
    values.put(Constants.CONTENT_NAME, wishy.getContent());
    values.put(Constants.DATE_NAME, System.currentTimeMillis());

    Log.v("DatabaseHandler","SQLite creating....");

    SQLiteDatabase dba = this.getWritableDatabase(); //APP CRASHES HERE

    Log.v("DatabaseHandler","SQLite created");
    dba.insert(Constants.TABLE_NAME, null, values);
    //db.insert(Constants.TABLE_NAME, null, values);

    Log.v("Wish successfully!", "yeah!!");
    dba.close();
}

public ArrayList<MyWish> getWishes() {
    wishList.clear();
    String selectQuery = "SELECT * FROM " + Constants.TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    // Cursor cursor = db.rawQuery(selectQuery, null);

    Cursor cursor = db.query(Constants.TABLE_NAME, new String[]{Constants.KEY_ID, Constants.TITLE_NAME, Constants.CONTENT_NAME, Constants.DATE_NAME},null,null, null,null,Constants.DATE_NAME+ " DESC");

    //loop through cursor
    if (cursor.moveToFirst()) {

        do {

            MyWish wish = new MyWish();
            wish.setTitle(cursor.getString(cursor.getColumnIndex(Constants.TITLE_NAME)));
            wish.setContent(cursor.getString(cursor.getColumnIndex(Constants.CONTENT_NAME)));

            java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance();
            String dataData = dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Constants.DATE_NAME))).getTime());

            wish.setRecorddate(dataData);

            wishList.add(wish);

        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return wishList;

    }
}

我的主要活动是:

public class MainActivity extends AppCompatActivity {

private EditText title;
private EditText content;
private Button saveButton;
private DatabaseHandler dba;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.v("Main Activity","Instantiating Database");
    dba=new DatabaseHandler(MainActivity.this);
    Log.v("Main Activity","Instantiated Database");
    title=(EditText)findViewById(R.id.editText);
    content=(EditText)findViewById(R.id.editText2);
    saveButton=(Button)findViewById(R.id.button);

    Log.v("Main Activity","About to click");
    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           // Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
           // Toast.makeText(getApplicationContext(),"Object Instantiated",Toast.LENGTH_LONG).show();

            MyWish wish=new MyWish();
            wish.setTitle(title.getText().toString().trim());
            wish.setContent(content.getText().toString().trim());

           // Toast.makeText(getApplicationContext(),"Object created",Toast.LENGTH_LONG).show();
            Log.v("Main Activity","Adding wish");
            dba.addWishes(wish);
          //  Toast.makeText(getApplicationContext(),"Object Adding",Toast.LENGTH_LONG).show();
            dba.close();

            title.setText("");
            content.setText("");

            // Intent i= new Intent(MainActivity.this,WishDetailActivity.class);
            //startActivity(i);
        }
    });


  }
}

我已将所有String值放在Contents类

public class Constants {
public static final String DATABASE_NAME="wishdb";
public static final int DATABASE_VERSION=1;
public static final String TABLE_NAME="wishes";
public static final String KEY_ID="+id";
public static final String TITLE_NAME="title";
public static final String CONTENT_NAME="content";
public static final String DATE_NAME="recorddate";
}

崩溃日志是:

05-25 12:54:37.801 26777-26777/edittext.com.sqlite4 V/Main Activity: Adding wish
05-25 12:54:37.801 26777-26777/edittext.com.sqlite4 V/ADDING WISH TO DATABASE: ADDING!!!!!!!
05-25 12:54:37.801 26777-26777/edittext.com.sqlite4 V/DatabaseHandler: SQLite generated
05-25 12:54:37.801 26777-26777/edittext.com.sqlite4 V/DatabaseHandler: SQLite creating....
05-25 12:54:37.805 26777-26777/edittext.com.sqlite4 E/SQLiteLog: (1) near "+": syntax error
05-25 12:54:37.805 26777-26777/edittext.com.sqlite4 D/AndroidRuntime: Shutting down VM
05-25 12:54:37.805 26777-26777/edittext.com.sqlite4 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4cd5b20)
05-25 12:54:37.805 26777-26777/edittext.com.sqlite4 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: edittext.com.sqlite4, PID: 26777
                                                                      android.database.sqlite.SQLiteException: near "+": syntax error (code 1): , while compiling: CREATE TABLE wishes(+id INTEGER PRIMARY KEY, title TEXT, content TEXT, recorddate LONG);
                                                                          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.executeSql(SQLiteDatabase.java:1672)
                                                                          at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
                                                                          at data.DatabaseHandler.onCreate(DatabaseHandler.java:34)
                                                                          at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
                                                                          at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                          at data.DatabaseHandler.addWishes(DatabaseHandler.java:62)
                                                                          at edittext.com.sqlite4.MainActivity$1.onClick(MainActivity.java:54)
                                                                          at android.view.View.performClick(View.java:4438)
                                                                          at android.view.View$PerformClick.run(View.java:18422)
                                                                          at android.os.Handler.handleCallback(Handler.java:733)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                          at dalvik.system.NativeStart.main(Native Method)
05-25 12:54:39.201 26777-26777/edittext.com.sqlite4 I/Process: Sending signal. PID: 26777 SIG: 9

0 个答案:

没有答案