sqlite数据库只插入一列数据
Only one column of data inserted in the sqlite database
所以我使用多个 radiogroup
和一个 EditText
将数据插入数据库。问题是只插入了一列数据,而且它始终是 collection_time
列。
这是插入数据的编码:
BtnNext1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SelectPrintingDetails.this, Summary.class);
int selectedId = rgCP.getCheckedRadioButtonId();
if(selectedId != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId);
String rbCP = selectedRadioButton.getText().toString();
Boolean insertPD = db.insertcolor(rbCP);
if(insertPD == true)
{
intent.putExtra("Color", rbCP);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
int selectedId1 = rgFP.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId1);
String rbFP = selectedRadioButton.getText().toString();
Boolean insertPD1 = db.insertflip(rbFP);
if(insertPD1 == true)
{
intent.putExtra("Flip", rbFP);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
int selectedId2 = rgPC.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId2);
String rbPC = selectedRadioButton.getText().toString();
Boolean insertPD2 = db.insertpc(rbPC);
if(insertPD2 == true)
{
intent.putExtra("Plastic", rbPC);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
int selectedId3 = rgBind.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId3);
String rbBind = selectedRadioButton.getText().toString();
Boolean insertPD3 = db.insertbind(rbBind);
if(insertPD3 == true)
{
intent.putExtra("Bind", rbBind);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
String timecollect = etCT.getText().toString();
Boolean insertPD4 = db.insertct(timecollect);
if(insertPD4 == true)
{
intent.putExtra("Collect", timecollect);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
startActivity(intent);
}
});
这是数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "hdatabase1.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table printingdetails (color text, binding text, collection_time text, flipped_page text, plastic_cover text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("drop table if exists printingdetails");
}
public boolean insertcolor(String color)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("color", color);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertflip(String flipped_page)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("flipped_page", flipped_page);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertpc(String plastic_cover)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("plastic_cover", plastic_cover);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertbind(String binding)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("binding", binding);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertct (String collection_time)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("collection_time", collection_time);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
希望大家指导我如何在每一列中插入所有数据。我还是新手,我已经在这里参考了几个相关的 post,其中 none 确实对
有帮助
目前您似乎是分别插入每一列的值。这意味着您最终得到的不是单行,而是多行,其中只有一列有值,其余为空。
为了解决这个问题,您需要一个类似这样的方法:
public boolean insertRow(String color, String binding, String time, String flipped_page, String plastic_cover) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("color", color);
contentValues.put("binding", binding);
contentValues.put("collection_time", time);
contentValues.put("flipped_page", flipped_page);
contentValues.put("plastic_cover", plastic_cover);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
应删除每个单独的插入命令。现在,您的 onClick
方法必须检索所有选定的项目,然后调用此插入方法。未选择的无线电组的处理将取决于您的应用程序。在下面的示例中,我只是中断了方法,从而取消了提交。如果某些单选组是可选的,那么可以将相应的字符串设置为空(""
)并继续该方法,而不是通过返回取消提交。
public void onClick(View v) {
Intent intent = new Intent(SelectPrintingDetails.this, Summary.class);
String color, binding, time, page, cover;
int selectedId = rgCP.getCheckedRadioButtonId();
if(selectedId != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId);
color = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "Color is not selected",Toast.LENGTH_SHORT).show();
return; // this prevents the rest of the code from executing
}
int selectedId1 = rgFP.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId1);
page = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "FlipPage is not selected",Toast.LENGTH_SHORT).show();
return;
}
int selectedId2 = rgPC.getCheckedRadioButtonId();
if(selectedId2 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId2);
cover = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "Plastic Cover is not selected",Toast.LENGTH_SHORT).show();
return;
}
int selectedId3 = rgBind.getCheckedRadioButtonId();
if(selectedId3 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId3);
binding = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "Binding is not selected",Toast.LENGTH_SHORT).show();
return;
}
time = etCT.getText().toString();
// since all attributed have been read, now we call insert
// you can read the returned boolean to determine if the insertion was successful
db.insertRow(color, binding, time, page, cover);
// Also add all of them as intent extras
intent.putExtra("Color", color);
intent.putExtra("Bind", binding);
intent.putExtra("Collect", time);
intent.putExtra("Flip", page);
intent.putExtra("Plastic", cover);
startActivity(intent);
}
我希望这能帮助您了解哪里出了问题,以及您如何才能继续前进。
所以我使用多个 radiogroup
和一个 EditText
将数据插入数据库。问题是只插入了一列数据,而且它始终是 collection_time
列。
这是插入数据的编码:
BtnNext1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SelectPrintingDetails.this, Summary.class);
int selectedId = rgCP.getCheckedRadioButtonId();
if(selectedId != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId);
String rbCP = selectedRadioButton.getText().toString();
Boolean insertPD = db.insertcolor(rbCP);
if(insertPD == true)
{
intent.putExtra("Color", rbCP);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
int selectedId1 = rgFP.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId1);
String rbFP = selectedRadioButton.getText().toString();
Boolean insertPD1 = db.insertflip(rbFP);
if(insertPD1 == true)
{
intent.putExtra("Flip", rbFP);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
int selectedId2 = rgPC.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId2);
String rbPC = selectedRadioButton.getText().toString();
Boolean insertPD2 = db.insertpc(rbPC);
if(insertPD2 == true)
{
intent.putExtra("Plastic", rbPC);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
int selectedId3 = rgBind.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId3);
String rbBind = selectedRadioButton.getText().toString();
Boolean insertPD3 = db.insertbind(rbBind);
if(insertPD3 == true)
{
intent.putExtra("Bind", rbBind);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Radiobutton is not selected",Toast.LENGTH_SHORT).show();
}
String timecollect = etCT.getText().toString();
Boolean insertPD4 = db.insertct(timecollect);
if(insertPD4 == true)
{
intent.putExtra("Collect", timecollect);
}
else
{
Toast.makeText(getApplicationContext(), "Data is not inserted",Toast.LENGTH_SHORT).show();
}
startActivity(intent);
}
});
这是数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "hdatabase1.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table printingdetails (color text, binding text, collection_time text, flipped_page text, plastic_cover text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("drop table if exists printingdetails");
}
public boolean insertcolor(String color)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("color", color);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertflip(String flipped_page)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("flipped_page", flipped_page);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertpc(String plastic_cover)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("plastic_cover", plastic_cover);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertbind(String binding)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("binding", binding);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
public boolean insertct (String collection_time)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("collection_time", collection_time);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
希望大家指导我如何在每一列中插入所有数据。我还是新手,我已经在这里参考了几个相关的 post,其中 none 确实对
有帮助目前您似乎是分别插入每一列的值。这意味着您最终得到的不是单行,而是多行,其中只有一列有值,其余为空。
为了解决这个问题,您需要一个类似这样的方法:
public boolean insertRow(String color, String binding, String time, String flipped_page, String plastic_cover) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("color", color);
contentValues.put("binding", binding);
contentValues.put("collection_time", time);
contentValues.put("flipped_page", flipped_page);
contentValues.put("plastic_cover", plastic_cover);
long ins = db.insert("printingdetails", null, contentValues);
if(ins==-1) return false;
else return true;
}
应删除每个单独的插入命令。现在,您的 onClick
方法必须检索所有选定的项目,然后调用此插入方法。未选择的无线电组的处理将取决于您的应用程序。在下面的示例中,我只是中断了方法,从而取消了提交。如果某些单选组是可选的,那么可以将相应的字符串设置为空(""
)并继续该方法,而不是通过返回取消提交。
public void onClick(View v) {
Intent intent = new Intent(SelectPrintingDetails.this, Summary.class);
String color, binding, time, page, cover;
int selectedId = rgCP.getCheckedRadioButtonId();
if(selectedId != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId);
color = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "Color is not selected",Toast.LENGTH_SHORT).show();
return; // this prevents the rest of the code from executing
}
int selectedId1 = rgFP.getCheckedRadioButtonId();
if(selectedId1 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId1);
page = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "FlipPage is not selected",Toast.LENGTH_SHORT).show();
return;
}
int selectedId2 = rgPC.getCheckedRadioButtonId();
if(selectedId2 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId2);
cover = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "Plastic Cover is not selected",Toast.LENGTH_SHORT).show();
return;
}
int selectedId3 = rgBind.getCheckedRadioButtonId();
if(selectedId3 != -1)
{
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedId3);
binding = selectedRadioButton.getText().toString();
}
else
{
Toast.makeText(getApplicationContext(), "Binding is not selected",Toast.LENGTH_SHORT).show();
return;
}
time = etCT.getText().toString();
// since all attributed have been read, now we call insert
// you can read the returned boolean to determine if the insertion was successful
db.insertRow(color, binding, time, page, cover);
// Also add all of them as intent extras
intent.putExtra("Color", color);
intent.putExtra("Bind", binding);
intent.putExtra("Collect", time);
intent.putExtra("Flip", page);
intent.putExtra("Plastic", cover);
startActivity(intent);
}
我希望这能帮助您了解哪里出了问题,以及您如何才能继续前进。