双引号未正确关闭字符串文字

时间:2011-01-31 09:45:04

标签: android sql database string

嘿大家;) 我有以下代码:

private void onCreateDBAndDBTables(){
 myDB = this.openOrCreateDatabase("Projects", MODE_PRIVATE, null);
 myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE 
   + "( _id integer primary key autoincrement
     name varchar (16),
     comment varchar (128),
     customer_project boolean,
     booking_details varchar (225),
     editable boolean )"  
       +";");
}

它显示标题中提到的错误。 我希望你能在这里帮助我,因为我不知道出了什么问题...... 〜SAM〜

4 个答案:

答案 0 :(得分:3)

您需要连接的字符串中的每一个新行:

private void onCreateDBAndDBTables(){
 myDB = this.openOrCreateDatabase("Projects", MODE_PRIVATE, null);
 myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE +
              "( _id integer primary key," +
              "name varchar (16)," +
              "comment varchar (128)," +
              "customer_project boolean," +
              "booking_details varchar (225)," +
              "editable boolean )" +  
              ";");
}

*更新*

我还注意到你使用的是SQLite3,所以我稍微更新了SQL - 删除了autoincrement

查看这篇关于SQLite3中自动增量如何工作的文章:

http://www.sqlite.org/faq.html#q1

答案 1 :(得分:0)

我猜你在自动增量后缺少一个逗号(它应该是auto_increment)。并且SQL中没有类型布尔值。您考虑过使用SQLiteOpenHelper课吗? Notepad Tutorial中包含一个示例。

答案 2 :(得分:0)

我认为您在autoincrement之后缺少逗号,然后您不需要使用尾随分号

答案 3 :(得分:0)

这是我的完整代码! :

import java.util.Arrays;

import datenbank.test.R.id; import android.R.layout; import android.app.Activity; import android.content.SharedPreferences.Editor; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; import android.widget.AbsSpinner; import android.widget.Toast;

公共类Projects_new扩展了Activity {

SQLiteDatabase myDB = null;
  static final int MENU_INSERT_PROJECTS = 0;
  static final int MENU_UPDATE_PROJECTS = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(layout.activity_list_item);

    Spinner s1 = (Spinner) findViewById(R.id.PROJECTS_NEW);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, 
            (MENU_INSERT_PROJECTS),
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(adapter);

    if(getIntent().hasExtra("id") == true) {
        long l = getIntent().getExtras().getLong("id");
        myDB = this.openOrCreateDatabase(datenbank.MY_DB_NAME, MODE_PRIVATE, null);

        Cursor myCursor = myDB.rawQuery("SELECT name,comment, customer_project, booking_details, editable FROM" + 
                datenbank.MY_DB_TABLE + "WHERE _id = "+l+";", null);

        startManagingCursor(myCursor);

        int ColumnName = myCursor.getColumnIndex("name"); //Name
        int ColumnComment = myCursor.getColumnIndex("Comment"); //Comment
        int ColumnCustomer_Project = myCursor.getColumnIndex ("Customer-Project?");//ist es für customer oder intern
        int ColumnBooking_Details = myCursor.getColumnIndex("Details"); //details
        int ColumnEditable = myCursor.getColumnIndex("Editable?"); //bearbeiten ja oder nein

         if (myCursor != null) {
                if (myCursor != null) { 
                    EditText eName = (EditText)findViewById(R.id.PROJECTS_NEW);
                    eName.setText(myCursor.getString(ColumnName));

                    EditText eComment = (EditText)findViewById(R.id.PROJECTS_NEW);
                    eComment.setText(myCursor.getString(ColumnComment));

                    EditText eCustomer_Project = (EditText)findViewById(R.id.PROJECTS_NEW);
                    eCustomer_Project.setText(myCursor.getString(ColumnCustomer_Project));


                    Spinner sBooking_Details = (Spinner)findViewById(R.id.PROJECTS_NEW);
                    sBooking_Details.setSelection(myCursor.getInt(ColumnBooking_Details), true);

                    EditText eEditable = (EditText)findViewById(R.id.PROJECTS_NEW);
                    eEditable.setText(myCursor.getString(ColumnEditable));
                }
  }}
}

  private void setContentView(Class<layout> class1) {
    // TODO Auto-generated method stub

}

private EditText findViewById(id id) {
    // TODO Auto-generated method stub
    return null;
}

public boolean onCreateOptionsMenu(Menu menu) {

      super.onCreateOptionsMenu(menu);

      if(getIntent().hasExtra("id")==true)
      {
          menu.add(0, MENU_UPDATE_PROJECTS, 0, R.string.MENU_PROJECTS)
          .setShortcut('1','s')
          .setIcon(android.R.drawable.ic_menu_save);

      }
      else
      {
          menu.add(0, MENU_INSERT_PROJECTS, 0, R.string.MENU_PROJECTS)
          .setShortcut('1','s')
          .setIcon(android.R.drawable.ic_menu_save);
      }
      return true;

  }

  public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
        case MENU_INSERT_PROJECTS:
            EditText Name = (EditText)findViewById(R.id.ed_Name);
            EditText Comment = (EditText)findViewById(R.id.ed_Comment);
            EditText CustomerProject = (EditText)findViewById(R.id.ed_CustomerProject);
            EditText BookingDetails = (EditText)findViewById(R.id.ed_BookingDetails);
            Spinner Editable = (Spinner)findViewById(R.id.cb_Editable);

            int i = Editable.getSelectedItemPosition();

            if(Name.length()!=0){
                myDB = this.openOrCreateDatabase(datenbank.MY_DB_NAME,
                        MODE_PRIVATE, null);


            }

            //Projekt-Daten updaten (UPDATE)        
            if(getIntent().hasExtra("id") == true)
            {
                long l = getIntent().getExtras().getLong("id");
                myDB.execSQL("UPDATE "+datenbank.MY_DB_TABLE+" SET "+
                        "name = '"+Name.getText().toString()+"', "+
                    "Comment = '"+Comment.getText().toString()+"', "+
                    "BookingDetails ='"+BookingDetails.getText().toString()+"', "+
                    "Editable ='"+i+"', "+
                    "CustomerProject = '"+CustomerProject.getText().toString()+"' "+
                    "WHERE _id = "+l+";"); 
            }

            //Neues Projekt in Datenbank speichern (INSERT)             
            else
            {                   
                myDB.execSQL("INSERT INTO" +datenbank.MY_DB_TABLE +"
                                                                  (Name,  
                                                                  Comment,
                                                                  BookingDetails,
                                                                  Editable,  
                                                                  CustomerProject) 
                        +"VALUES ('"+Name.getText().toString()+"',"+
                        "'"+Comment.getText().toString()+"',"+
                        "'"+BookingDetails.getText().toString()+"',"+
                        "'"+i+"',"+"'"+CustomerProject.getText().toString()+"');");
            }
            finish();
            return true;    
        }
        else
        {
            Toast toast = Toast.makeText(this, "Please enter a name for the new Project!", 
                                                               Toast.LENGTH_SHORT);
            toast.show();
        }

    return false;
}

}