Hey everyone ;) I have the following code:
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 )"
+";");
}
and it shows the error as mentioned in the Title. I hope you can help me out here, because I have no开发者_C百科 idea what's wrong... ~sam~
Every new line in the string you need to concatenate:
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 )" +
";");
}
* Update *
I also noticed that you were using SQLite3, so I updated the SQL a bit - removed the autoincrement
.
Check out this article on how auto increment works in SQLite3:
http://www.sqlite.org/faq.html#q1
I guess you are missing a comma after autoincrement (it should be auto_increment). And there is no type boolean in SQL. Have you considered using the SQLiteOpenHelper
class? An example is included in the Notepad Tutorial.
I think you are missing a comma after autoincrement
and then you don't need the trailing semicolon
this is my full code! :
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;
public class Projects_new extends 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;
}
}
精彩评论