如何添加自动增量ID?

时间:2018-05-23 16:14:37

标签: android android-sqlite

我正在尝试将AutoIncrement ID添加到我的数据库表中,并在用户选择“查看全部”时显示它。
但是,在添加到我的表后,应用程序将崩溃,我相信我正在错误地执行其中一个过程 以下代码是我当前的实现,没有尝试使用AUTOINCREMENT 如果有人可以协助这个初始化,我会非常感激;谢谢

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;
}

}

错误在第56行:

db.execSQL("INSERT INTO student VALUES('"+cost.getText()+"','"+make.getText()+"','"+model.getText()+"','"+fname.getText()+
                    "','"+lname.getText()+"');");

1 个答案:

答案 0 :(得分:1)

添加空;

从费用中删除单引号;

成功

db.execSQL("INSERT INTO student VALUES(null,"+cost.getText()+",'"+make.getText()+"','"+model.getText()+"','"+fname.getText()+
                "','"+lname.getText()+"');");