简体   繁体   English

(Android)无法将数据插入SQLite数据库

[英](Android) Can not insert data into SQLite database

I tried a lot of fixes but still can not find out why the data can not be inserted.I used a tool called BD Browser for SQLite to check the data and there are tables and columns but no values.. 我尝试了许多修复程序,但仍然无法找出为什么不能插入数据的原因。我使用了一个名为BD Browser的SQLite工具来检查数据,并且有表和列但没有值。

Codes: 代码:

public class MainActivity extends Activity {
private Button btnadd,btnselect;
private EditText etmiles,etdate,etcosts,etprice;

SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnadd=(Button)findViewById(R.id.add);
    btnselect=(Button)findViewById(R.id.select);
    etmiles=(EditText)findViewById(R.id.miles);
    etdate=(EditText)findViewById(R.id.date);
    etprice=(EditText)findViewById(R.id.price);
    etcosts=(EditText)findViewById(R.id.costs);
    db=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null);



    btnselect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent=new Intent(MainActivity.this,Activity02.class);
            startActivity(intent);

        }
    });
     btnadd.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


        String amiles=etmiles.getText().toString();
        String sdate=etdate.getText().toString();
        String sprice=etprice.getText().toString();
        String acosts=etcosts.getText().toString();
        if(etmiles.getText().toString().length() == 0||etdate.getText().toString().length() == 0||etprice.getText().toString().length() == 0||etcosts.getText().toString().length() == 0)
        {
            Toast toast=Toast.makeText(MainActivity.this, "xxxx", Toast.LENGTH_SHORT);
            toast.show();
            return;
        }
        else
        {

            try{
                db.execSQL("insert into test1('" + sdate + "','" + amiles +  "','" + sprice + "','" + acosts +  "')");

                }
                catch(SQLiteException e)
                {
                    db.execSQL("create table if not exists test1(sdate varchar(40) primary key,amiles varchar(30),sprice varchar(30),acosts varchar(30))");
                }        
        Toast toast=Toast.makeText(MainActivity.this, "xxxx", Toast.LENGTH_SHORT);
        toast.show();
        }
        }
    });

}

} }

Error message:near")":syntax error 错误消息:“”附近“)”:语法错误

It seems your INSERT statement doesn't match SQLite's syntax. 看来您的INSERT陈述式与SQLite的语法不符。 Try adding VALUES like so: 尝试像这样添加VALUES

INSERT INTO test1 VALUES (..., ..., ...)

And in truth on Android you should consider the insert() method instead. 实际上,在Android上,您应该考虑使用insert()方法。

you should use insert(String table, String nullColumnHack, ContentValues values) 您应该使用insert(String table,String nullColumnHack,ContentValues values)

where table is the name of the table you want to insert, nullColumnHack it is optional and can be null, and values is a map which contains the value we want to insert. 其中table是要插入的表的名称, nullColumnHack是可选的,可以为null,而value是包含我们要插入的值的映射。 The key of this map is the column name. 该映射的键是列名。 Eg 例如

ContentValues values = new ContentValues(); 
values.put(HISTORY_ORDER_ID_COL, orderId); 
values.put(HISTORY_PRODUCT_ID_COL, productId); 

long rowId = db.insert(TABLE_ORDER, null, values);

android will take care of writing the insert query for you. android将为您编写插入查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM