gpt4 book ai didi

java - Sqlite 数据库检索数据到 arraylist

转载 作者:行者123 更新时间:2023-11-30 02:21:17 25 4
gpt4 key购买 nike

<分区>

我想将所有数据库值放入我的数组列表中并显示在 logcat 中。但我在从 arraylist 获取值(value)时遇到了一些问题。我输入了我的漏洞代码,我能够获得值(value)但从模型中获得了最后的值(value)。

我的主要 Activity

package com.example.databasework;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button btnInsertCW, btnInsertCD, btnGetAllVAlue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInsertCW = (Button) findViewById(R.id.btnInsertCW);
btnInsertCD = (Button) findViewById(R.id.btnInsertCD);
btnGetAllVAlue = (Button) findViewById(R.id.btnGetAllVAlue);
btnInsertCW.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v1) {
// TODO Auto-generated method stub

Database transactionDb = new Database(getApplicationContext());
TransactionModel transactionModel = new TransactionModel();
transactionDb.open();
transactionModel.setDate("14/2/2015");
transactionModel.setTime("11:12:15");
transactionModel.setEnrollment("CW");
transactionDb.insert(transactionModel);
transactionDb.close();

}
});

btnInsertCD.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v2) {
// TODO Auto-generated method stub
Database transactionDb = new Database(getApplicationContext());
TransactionModel transactionModel = new TransactionModel();
transactionDb.open();
transactionModel.setDate("28/3/2014");
transactionModel.setTime("09:08:06");
transactionModel.setEnrollment("CD");
transactionDb.insert(transactionModel);
transactionDb.close();

}
});

btnGetAllVAlue.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v3) {
// TODO Auto-generated method stub

ArrayList<TransactionModel> arrayList = new ArrayList<TransactionModel>();

Database transactionDb = new Database(getApplicationContext());
transactionDb.open();
arrayList = transactionDb.getReportPrintTrns();
transactionDb.close();

System.err.println(arrayList);

for (int i = 0; i < arrayList.size(); i++)
{
TransactionModel transactionModel = arrayList.get(i);
System.err.println(transactionModel.getEnrollment());
}

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

在 arrayList = transactionDb.getReportPrintTrns();想要所有数据库值。

我的数据库

package com.example.databasework;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database {

private static final String DATABASE_NAME = "KisanMitra.db";
public static final String DATABASE_TABLE = "TransactionDb";
private static final int DATABASE_VERSION = 1;

public static final String TrnsNo = "TrnsNo";
public static final String Enrollment = "Enrollment";
public static final String CardNumber = "CardNumber";
public static final String EnrollmentAmount = "EnrollmentAmount";
public static final String Date = "Date";
public static final String Time = "Time";
public static final String ReturnStatus = "ReturnStatus";
public static final String TotalCashWithdrawalAmount = "TotalCashWithdrawalAmount";
public static final String TotalCashDepositAmount = "TotalCashDepositAmount";
public static final String TallyCash = "TallyCash";
public static final String KindPurchase = "KindPurchase";

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{

db.execSQL("create table " +DATABASE_TABLE + "("
+ TrnsNo + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ Enrollment + " text,"
+ Date + " text,"
+ CardNumber + " text,"
+ Time + " text,"
+ ReturnStatus + " text,"
+ EnrollmentAmount + " text,"
+ TotalCashDepositAmount + " text,"
+ TotalCashWithdrawalAmount + " text,"
+ KindPurchase + " text,"
+ TallyCash + " text);");

Log.e("TransationDb", "Create Succssfully");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;

String sql = null;
if (oldVersion == 1)
sql = "alter table " + DATABASE_TABLE + " add note text;";
if (oldVersion == 2)
sql = "";

Log.d("EventsData", "onUpgrade : " + sql);

if (sql != null)
db.execSQL(sql);
}

}
public Database open() throws SQLException {

ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}

public Database(Context c) {
ourContext = c;
}

public void close() {

ourHelper.close();
}

public void insert(TransactionModel TransactionSetter)
{
String iEnrollment = TransactionSetter.getEnrollment();
String iEnrollmentAmount = TransactionSetter.getEnrollmentAmount();
String iDate = TransactionSetter.getDate();
String iTime = TransactionSetter.getTime();
String iReturnStatus = TransactionSetter.getReturnStatus();
String iCardNumber = TransactionSetter.getCardNumber();
String iTotalCashWithdrawalAmount = TransactionSetter.getTotalCashWithdrawalAmount();
String iTotalCashDepositAmount = TransactionSetter.getTotalCashDepositAmount();
String iTallyCash = TransactionSetter.getTallyCash();
String iKindPurchase = TransactionSetter.getKindPurchase();

ContentValues contentValues = new ContentValues();

contentValues.put(Enrollment, iEnrollment);
contentValues.put(EnrollmentAmount, iEnrollmentAmount);
contentValues.put(Date, iDate);
contentValues.put(Time, iTime);
contentValues.put(ReturnStatus, iReturnStatus);
contentValues.put(CardNumber, iCardNumber);
contentValues.put(TotalCashWithdrawalAmount,iTotalCashWithdrawalAmount);
contentValues.put(TotalCashDepositAmount, iTotalCashDepositAmount);
contentValues.put(TallyCash, iTallyCash);
contentValues.put(KindPurchase, iKindPurchase);

ourDatabase.insert(DATABASE_TABLE, null, contentValues);
}


public ArrayList<TransactionModel> getReportPrintTrns() {
ArrayList<TransactionModel> list = new ArrayList<TransactionModel>();

TransactionModel model = new TransactionModel();

String whereClause = Enrollment + " in ('CW', 'CD')";

String[] columns = new String[] { TrnsNo, Enrollment, EnrollmentAmount,
Date, Time, ReturnStatus, CardNumber,
TotalCashWithdrawalAmount, TotalCashDepositAmount, TallyCash, KindPurchase };

Cursor c=ourDatabase.query(DATABASE_TABLE, columns, whereClause,null,null,null,null);

int itransno = c.getColumnIndex(TrnsNo);
int iEnrollment = c.getColumnIndex(Enrollment);
int iEnrollmentAmount = c.getColumnIndex(EnrollmentAmount);
int iDate = c.getColumnIndex(Date);
int iTime = c.getColumnIndex(Time);
int iReturnStatus = c.getColumnIndex(ReturnStatus);
int iCardNumber = c.getColumnIndex(CardNumber);
int iTotalCashWithdrawalAmount = c.getColumnIndex(TotalCashWithdrawalAmount);
int iTotalCashDepositAmount = c.getColumnIndex(TotalCashDepositAmount);
int iTallyCash = c.getColumnIndex(TallyCash);
int iKindPurchase = c.getColumnIndex(KindPurchase);

if (c.moveToFirst()) {
do {
model.setTrnsNo(c.getString(itransno));
model.setEnrollment(c.getString(iEnrollment));
System.err.println("iEnrollmentType :- "+c.getString(iEnrollment));
model.setEnrollmentAmount(c.getString(iEnrollmentAmount));
model.setDate(c.getString(iDate));
model.setTime(c.getString(iTime));
model.setReturnStatus(c.getString(iReturnStatus));
model.setCardNumber(c.getString(iCardNumber));
model.setTotalCashWithdrawalAmount(c.getString(iTotalCashWithdrawalAmount));
model.setTotalCashDepositAmount(c.getString(iTotalCashDepositAmount));
model.setTallyCash(c.getString(iTallyCash));
model.setKindPurchase(c.getString(iKindPurchase));
list.add(model);
} while (c.moveToNext());

}

c.close();
c.deactivate();
return list;
}

}

我的 SetGet 模型

package com.example.databasework;

public class TransactionModel {
String TrnsNo, Enrollment, EnrollmentAmount, Date, Time, ReturnStatus,
CardNumber, TotalCashWithdrawalAmount,
TotalCashDepositAmount, TallyCash, KindPurchase;

public TransactionModel() {
// TODO Auto-generated constructor stub
TrnsNo = "";
Enrollment = "";
EnrollmentAmount = "";
Date = "";
ReturnStatus = "";
Time = "";
CardNumber = "";
TotalCashWithdrawalAmount = "";
TotalCashDepositAmount = "";
TallyCash = "";
KindPurchase = "";
}

public String getTrnsNo() {
return TrnsNo;
}

public void setTrnsNo(String trnsNo) {
TrnsNo = trnsNo;
}

public String getEnrollment() {
return Enrollment;
}

public void setEnrollment(String enrollment) {
Enrollment = enrollment;
}

public String getEnrollmentAmount() {
return EnrollmentAmount;
}

public void setEnrollmentAmount(String enrollmentAmount) {
EnrollmentAmount = enrollmentAmount;
}

public String getDate() {
return Date;
}

public void setDate(String date) {
Date = date;
}

public String getTime() {
return Time;
}

public void setTime(String time) {
Time = time;
}

public String getReturnStatus() {
return ReturnStatus;
}

public void setReturnStatus(String returnStatus) {
ReturnStatus = returnStatus;
}

public String getCardNumber() {
return CardNumber;
}

public void setCardNumber(String cardNumber) {
CardNumber = cardNumber;
}

public String getTotalCashWithdrawalAmount() {
return TotalCashWithdrawalAmount;
}

public void setTotalCashWithdrawalAmount(String totalCashWithdrawalAmount) {
TotalCashWithdrawalAmount = totalCashWithdrawalAmount;
}

public String getTotalCashDepositAmount() {
return TotalCashDepositAmount;
}

public void setTotalCashDepositAmount(String totalCashDepositAmount) {
TotalCashDepositAmount = totalCashDepositAmount;
}

public String getTallyCash() {
return TallyCash;
}

public void setTallyCash(String tallyCash) {
TallyCash = tallyCash;
}

public String getKindPurchase() {
return KindPurchase;
}

public void setKindPurchase(String kindPurchase) {
KindPurchase = kindPurchase;
}


}

获得值(value)

 02-14 10:31:23.620: W/System.err(10564): iEnrollmentType :- CW
02-14 10:31:23.620: W/System.err(10564): iEnrollmentType :- CD
02-14 10:31:23.620: W/System.err(10564): iEnrollmentType :- CW
02-14 10:31:23.620: W/System.err(10564): iEnrollmentType :- CW
02-14 10:31:23.620: W/System.err(10564): iEnrollmentType :- CD



02-14 10:31:23.631: W/System.err(10564): CD
02-14 10:31:23.640: W/System.err(10564): CD
02-14 10:31:23.640: W/System.err(10564): CD
02-14 10:31:23.640: W/System.err(10564): CD
02-14 10:31:23.640: W/System.err(10564): CD

在最后一行中,我得到了相同的值,但数据库的值类似于

 CW
CD
CW
CW
CD

25 4 0