Reputation: 196
I am attempting to add an AutoIncrement ID to my database table, and also display it when the user selects, "View All".
However, after adding to my table the application will crash, I believe that I am doing one of these processes incorrectly.
The following code is my current implementation without my attempt at using AUTOINCREMENT.
If anyone can assist in this initialization I would greatly appreciate it; thank you
public class MainActivity extends Activity {
EditText fname,lname,cost,make,model;
Button add,view,viewall,Show1,delete,modify;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fname=(EditText)findViewById(R.id.firstname);
lname=(EditText)findViewById(R.id.lastname);
cost=(EditText)findViewById(R.id.cost);
make=(EditText)findViewById(R.id.make);
model=(EditText)findViewById(R.id.model);
add=(Button)findViewById(R.id.addbtn);
viewall=(Button)findViewById(R.id.viewallbtn);
delete=(Button)findViewById(R.id.deletebtn);
Show1=(Button)findViewById(R.id.showbtn);
modify=(Button)findViewById(R.id.modifybtn);
db=openOrCreateDatabase("CARDEALER", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(id INTEGER PRIMARY KEY AUTOINCREMENT,cost INTEGER,make VARCHAR, model VARCHAR, fname VARCHAR, lname VARCHAR);");
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cost.getText().toString().trim().length()==0||
make.getText().toString().trim().length()==0||
model.getText().toString().trim().length()==0||
fname.getText().toString().trim().length()==0||
lname.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('"+id.getText()+"','"+cost.getText()+"','"+make.getText()+"','"+model.getText()+"','"+fname.getText()+
"','"+lname.getText()+"');");
showMessage("Success", "Record added successfully");
clearText();
}
});
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cost.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Cost");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE cost='"+cost.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE cost='"+cost.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Cost");
}
clearText();
}
});
modify.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cost.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Cost");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE cost='"+cost.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE student SET make='"+make.getText()+"',model='"+model.getText()+"',fname='"+fname.getText()+"',lname='"+lname.getText()+
"' WHERE cost='"+cost.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Cost");
}
clearText();
}
});
viewall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("ID: "+c.getString(0)+"\n");
buffer.append("Cost: "+c.getString(1)+"\n");
buffer.append("Make: "+c.getString(2)+"\n");
buffer.append("Model: "+c.getString(3)+"\n");
buffer.append("First Name: "+c.getString(4)+"\n");
buffer.append("Last Name: "+c.getString(5)+"\n\n");
}
showMessage("Customer Detail", buffer.toString());
}
});
Show1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showMessage("Customer Management Application", "Developed By Tanner Tattini");
}
});
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
cost.setText("");
make.setText("");
model.setText("");
fname.setText("");
lname.setText("");
cost.requestFocus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.customer_main, menu);
return true;
}
}
Error is on line 56:
db.execSQL("INSERT INTO student VALUES('"+cost.getText()+"','"+make.getText()+"','"+model.getText()+"','"+fname.getText()+
"','"+lname.getText()+"');");
Upvotes: 0
Views: 263
Reputation: 1167
Add null;
remove the single quotation marks from the cost;
Make it
db.execSQL("INSERT INTO student VALUES(null,"+cost.getText()+",'"+make.getText()+"','"+model.getText()+"','"+fname.getText()+
"','"+lname.getText()+"');");
Upvotes: 1