我在sqlite数据库中工作我有一个产品表,其中包含产品ID,名称,价格,公司我将id作为主键和自动增量当我在数据库中添加产品时productid似乎是空 这里是我的数据库的照片 自动增量无法在SQLite数据库中工作
,这里是我的数据库类
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);
}
});
}
}
不错的解释谢谢 –