我在sqlite数据库中工作。我有一个产品表,其中包含产品ID,名称,价格和公司ID,当我将产品添加到数据库时,我将其作为主键和自动增量,但productid似乎是空的
这是我的数据库类
package com.example.abdelmagied.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by AbdELMagied on 7/24/2017.
*/
public class database extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "product.db";
private static final String TABLE_NAME = "myproduct";
/// columns in the database
private static final String COL1_NAME = "productId";
private static final String COL2_NAME = "productSalary";
private static final String COL3_NAME = "companyname";
private static final String COL4_NAME = "productName";
// constructor
public database(Context context) {
super(context, DATABASE_NAME , null , 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table myproduct ("+COL1_NAME+" integer primary key autoincrement, "+COL2_NAME+" text , "+COL3_NAME+" text , "+COL4_NAME+" text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("drop database " + DATABASE_NAME );
onCreate(sqLiteDatabase);
}
}
这是我在数据库中插入的活动
package com.example.abdelmagied.myapplication;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class addproduct extends AppCompatActivity {
public Button AddToDatabase;
public EditText productName , productSalary , companyMade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addproduct);
// get the button id
AddToDatabase = (Button) findViewById(R.id.AddDatabase);
// handle the action of the click
AddToDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// get the entered info from the edittexts
productName = (EditText) findViewById(R.id.PNameId);
productSalary =(EditText) findViewById(R.id.PSalaryId);
companyMade = (EditText) findViewById(R.id.PSalaryId);
//add to database productName , productSalary , companyMade...
database mydata = new database(getApplicationContext());
SQLiteDatabase db = mydata.getWritableDatabase();
ContentValues content = new ContentValues();
content.put("productSalary" , productSalary.getText().toString());
content.put("companyname" , companyMade.getText().toString());
content.put("productName" , productName.getText().toString());
db.insert("myproduct" , null , content);
}
});
}
}
答案 0 :(得分:0)
使用SQL创建表格,即
`sqLiteDatabase.execSQL("create table myproduct ("+COL1_NAME+" integer primary key autoincrement, "+COL2_NAME+" text , "+COL3_NAME+" text , "+COL4_NAME+" text)");`
然后使用以下内容进行测试: -
dbhlp = new SO45290855(this);
SQLiteDatabase dbx = dbhlp.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(SO45290855.COL2_NAME,"xxxx");
cv.put(SO45290855.COL3_NAME,"yyyy");
cv.put(SO45290855.COL4_NAME,"zzzz");
dbx.insert(SO45290855.TABLE_NAME,null,cv);
ContentValues cv2 = new ContentValues();
cv2.put(SO45290855.COL2_NAME,"xxxx");
cv2.put(SO45290855.COL3_NAME,"yyyy");
cv2.put(SO45290855.COL4_NAME,"zzzz");
dbx.insert(SO45290855.TABLE_NAME,null,cv);
Cursor csr = dbx.query(SO45290855.TABLE_NAME,null,null,null,null,null,null);
while (csr.moveToNext()) {
for (int i=0; i < csr.getColumnCount();i++) {
Log.d("CsrPos_" + Integer.toString(csr.getPosition()),
"Column=" + csr.getColumnName(i) +
" Data=" + csr.getString(i)
);
}
}
给出的输出是: -
07-25 09:30:41.721 2965-2965/? D/CsrPos_0: Column=productId Data=1
07-25 09:30:41.721 2965-2965/? D/CsrPos_0: Column=productSalary Data=xxxx
07-25 09:30:41.721 2965-2965/? D/CsrPos_0: Column=companyname Data=yyyy
07-25 09:30:41.721 2965-2965/? D/CsrPos_0: Column=productName Data=zzzz
07-25 09:30:41.721 2965-2965/? D/CsrPos_1: Column=productId Data=2
07-25 09:30:41.721 2965-2965/? D/CsrPos_1: Column=productSalary Data=xxxx
07-25 09:30:41.721 2965-2965/? D/CsrPos_1: Column=companyname Data=yyyy
07-25 09:30:41.721 2965-2965/? D/CsrPos_1: Column=productName Data=zzzz
SO45290855 是您数据库类的副本
因此,由于第一行显示 productId 的值为 1 ,并且第5行显示 porductId ,第二行的值为 2 ,原则上您所拥有的代码应该创建相同的。
因此,您很可能已修改代码以更改结构,但尚未意识到onCreate
基本上在数据库文件的生命周期内运行一次,因此除非您已删除数据库文件,否则更改为该结构将不适用。
可能的解决方案是清除应用的数据或卸载应用并重新运行。
除此之外,您很可能不需要使用column_name INTEGER PRIMARY KEY
编码AUTOINCREMENT将导致增加列。
我建议阅读包含
的SQLite AutoincrementAUTOINCREMENT关键字会占用额外的CPU,内存,磁盘空间和 磁盘I / O开销,如果不是严格需要,应该避免。它是 通常不需要。