String account=editTextaccount.getText().toString(); String password=editTextpassword.getText().toString(); dbHelper=new DBHelper(getActivity(),"user.db",null,1); db=dbHelper.getReadableDatabase(); db.execSQL("insert into user(account,password) values('account','password')");有没有错误
时间: 2024-02-02 10:04:27 浏览: 20
这段代码会有一个问题,即在SQL语句中插入的是字符串常量`'account'`和`'password'`,而不是变量`account`和`password`的值。正确的写法应该是使用占位符将变量的值绑定到SQL语句中的占位符上。修改后的代码如下:
```
String account = editTextaccount.getText().toString();
String password = editTextpassword.getText().toString();
dbHelper = new DBHelper(getActivity(), "user.db", null, 1);
db = dbHelper.getWritableDatabase();
String sql = "INSERT INTO user (account, password) VALUES (?, ?)";
db.execSQL(sql, new Object[]{account, password});
```
在上面的代码中,我们使用了占位符将变量`account`和`password`的值绑定到SQL语句中的占位符上,以避免SQL注入攻击。同时,我们使用了`getWritableDatabase()`方法获取可写数据库实例,以便插入数据。
相关问题
String namep = etProduct_name.getText().toString(); String pricep = etProduct_price.getText().toString(); String quantityp = etProduct_quantity.getText().toString(); ContentValues cv1 = new ContentValues(); cv1.put(DBHelper.COLUMN_NAME,namep); cv1.put(DBHelper.COLUMN_PRICE,pricep); cv1.put(DBHelper.COLUMN_QUANTITY,quantityp);这些怎么插入到上面的数据库
插入数据到数据库的步骤如下:
1. 获取可写数据库对象:调用 `getWritableDatabase()` 方法获取可写的数据库对象 `db`。
2. 创建 `ContentValues` 对象:`ContentValues` 对象是一个键值对集合,用于存储要插入的数据。这里创建一个 `ContentValues` 对象 `values`。
3. 将数据存入 `ContentValues` 对象:使用 `put()` 方法将要插入的数据存入 `ContentValues` 对象,键为列名,值为要插入的数据。
4. 插入数据:调用 `insert()` 方法将 `ContentValues` 对象中的数据插入到数据库中。第一个参数为表名,第二个参数为插入空值的列,第三个参数为 `ContentValues` 对象。
5. 关闭数据库:调用 `close()` 方法关闭数据库连接,释放资源。
根据你提供的代码,可以将数据插入到数据库中的方法如下:
```
public void insertData(String namep, String pricep, String quantityp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME, namep);
values.put(DBHelper.COLUMN_PRICE, pricep);
values.put(DBHelper.COLUMN_QUANTITY, quantityp);
db.insert(DBHelper.TABLE_NAME, null, values);
db.close();
}
```
在这个方法中,我们将三个参数 `namep`、`pricep` 和 `quantityp` 分别存入 `ContentValues` 对象 `values` 中,并调用 `insert()` 方法将数据插入到数据库表中。注意,这里使用了 `DBHelper` 类中定义的表名和列名。你可以将这个方法添加到你的 `DBHelper` 类中,然后在需要插入数据的地方调用它来将数据插入到数据库中。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_users); lvProductList = findViewById(R.id.product_list); etProductName = findViewById(R.id.product_name); etProductPrice = findViewById(R.id.product_price); etProductQuantity = findViewById(R.id.product_quantity); btAddProduct = findViewById(R.id.add_product); btDeleteProduct = findViewById(R.id.delete_product); btEditProduct = findViewById(R.id.edit_product); spinner = findViewById(R.id.spinner); dbHelper = new DBHelper(this); db = dbHelper.getWritableDatabase(); btAddProduct.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name = etProductName.getText().toString(); float price = Float.parseFloat(etProductPrice.getText().toString()); int quantity = Integer.parseInt(etProductQuantity.getText().toString()); ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_NAME,name); values.put(DBHelper.COLUMN_PRICE,price); values.put(DBHelper.COLUMN_QUANTITY,quantity); db.insertWithOnConflict(DBHelper.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_REPLACE); updateUI(); } });
这是 `users` 类中的 `onCreate()` 方法,用于初始化用户界面。在该方法中,首先调用 `setContentView()` 方法,将用户界面的布局文件加载到该活动中。接着,使用 `findViewById()` 方法获取布局文件中定义的 UI 控件,如 `ListView`、`EditText`、`Button`、`Spinner` 等等。然后,创建一个 `DBHelper` 对象,并调用 `getWritableDatabase()` 方法获取可写的数据库。接下来,为添加按钮 `btAddProduct` 添加一个点击事件监听器,在点击按钮时将用户输入的信息插入到数据库中,并调用 `updateUI()` 方法更新 ListView 中的数据。
在这个方法中,还涉及到了一些 Android 的基本操作,如从 `EditText` 中读取用户输入的信息、将信息存储到 `ContentValues` 对象中、使用 `insertWithOnConflict()` 方法将数据插入到数据库中、使用 `updateUI()` 方法更新 ListView 中的数据等等。