我试图连接到SQLite数据库,但我收到一个错误:Sqlexception:语法错误(code1)

时间:2015-08-19 17:11:58

标签: java android sql

我正在尝试连接到SQLite数据库,但是我收到了一个错误:Sqlexception:语法错误(code1)

public class MainActivity extends Activity {

    private EditText title_name,head_name,content;
    private Button saveButton;
    private DatabaseHandler dba;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dba=new DatabaseHandler(MainActivity.this);
        title_name=(EditText)findViewById(R.id.editText_Title);
        head_name=(EditText)findViewById(R.id.ediText_Name);
        content=(EditText)findViewById(R.id.editText_Content);
        saveButton=(Button)findViewById(R.id.button_Save);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveTodb();
            }
        });
    }

    private void saveTodb() {

        Mywish wish=new Mywish();
        wish.setTitle(title_name.getText().toString().trim());
        wish.setHeadingName(head_name.getText().toString().trim());
        wish.setContent(content.getText().toString().trim());
        dba.addWishes(wish);
        dba.close();

        title_name.setText("");
        head_name.setText("");
        content.setText("");
       // Intent i =new Intent(MainActivity.this,)
    }


}

然后我在这里声明数据库名称和表格

public class Constants {

    public static  final  String DATABASE_NAME="diary";
    public static  final  int DATABASE_VERSION=1;
    public static  final  String TABLE_NAME="details";
    public static  final  String TITLE_NAME="title";
    public static  final  String HEAD_NAME="headingname";
    public static  final  String CONTENT_NAME ="content";
    public static  final  String DATE_NAME="date";
    public static  final  String KEY_ID="_id";

}

这是我的DatabaseHandler类,我认为在尝试创建数据库表时发生了错误。

public class DatabaseHandler extends SQLiteOpenHelper{

    private final ArrayList<Mywish> wishList=new ArrayList<>();


    public DatabaseHandler(Context context ) {
        super(context,Constants.DATABASE_NAME,null,Constants.DATABASE_VERSION );
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

         String CREATE_DIARY_TABLE="CREATE TABLE"+Constants.TABLE_NAME + "("+Constants.KEY_ID +"INTEGER PRIMARY KEY,"+
                Constants.TITLE_NAME + "TEXT,"+Constants.HEAD_NAME +"TEXT,"+ Constants.CONTENT_NAME+"TEXT,"+Constants.DATE_NAME +"INTEGER)";
        db.execSQL(CREATE_DIARY_TABLE);


    }

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


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


    }

    public  void addWishes(Mywish wish)
    {
        SQLiteDatabase db=this.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(Constants.TITLE_NAME,wish.getTitle());
        values.put(Constants.HEAD_NAME,wish.getHeadingName());
        values.put(Constants.CONTENT_NAME,wish.getContent());
        values.put(Constants.DATE_NAME,java.lang.System.currentTimeMillis());
        db.insert(Constants.TABLE_NAME, null, values);
        Log.v("WISH SUCCESfuHOOO"," Hoo");
        db.close();
    }

    public ArrayList<Mywish> getWishes()
    {
        String selectQuery="SELECT * FROM"+Constants.TABLE_NAME;
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.query(Constants.TABLE_NAME,new String[]{Constants.KEY_ID,Constants.TITLE_NAME,Constants.HEAD_NAME,Constants.CONTENT_NAME,
       Constants.DATE_NAME },null,null,null,null,null,Constants.DATE_NAME+"DESC" );

              if (cursor.moveToFirst())
              {
                  do {

                      Mywish wish=new Mywish();
                      wish.setTitle(cursor.getString(cursor.getColumnIndex(Constants.TITLE_NAME)));
                      wish.setHeadingName(cursor.getString(cursor.getColumnIndex(Constants.HEAD_NAME)));
                      wish.setContent(cursor.getString(cursor.getColumnIndex(Constants.CONTENT_NAME)));
                      java.text.DateFormat dateFormat=java.text.DateFormat.getDateInstance();
                         String datedata=dateFormat.format(new Date(cursor.getLong(cursor.getColumnIndex(Constants.DATE_NAME))).getTime());
                        wish.setDateRecorded(datedata);
                      wishList.add(wish);
                  }while (cursor.moveToNext());
              }

        return wishList;
    }
}

这是我的DatabaseHandler类

public class Mywish {
    public String Title;
    public String DateRecorded;
    public  String Content;
    public  String HeadingName;

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
       this.Content = content;
    }

    public String getDateRecorded() {
        return DateRecorded;
    }

    public void setDateRecorded(String dateRecorded) {
       this.DateRecorded = dateRecorded;
    }

    public String getHeadingName() {
        return HeadingName;
    }

    public void setHeadingName(String headingName) {
        this.HeadingName = headingName;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
       this.Title = title;
    }
}

1 个答案:

答案 0 :(得分:3)

下面:

String CREATE_DIARY_TABLE="CREATE TABLE"+Constants.TABLE_NAME + "("+Constants.KEY_ID +"INTEGER PRIMARY KEY,"+
        Constants.TITLE_NAME + "TEXT,"+Constants.HEAD_NAME +"TEXT,"+ Constants.CONTENT_NAME+"TEXT,"+Constants.DATE_NAME +"INTEGER)";

你需要添加一些空格:

String CREATE_DIARY_TABLE="CREATE TABLE "+Constants.TABLE_NAME + "("+Constants.KEY_ID +" INTEGER PRIMARY KEY,"+
        Constants.TITLE_NAME + " TEXT,"+Constants.HEAD_NAME +" TEXT,"+ Constants.CONTENT_NAME+" TEXT,"+Constants.DATE_NAME +" INTEGER)";

或表格未能正确创建。