我是编程新手,开始了大约一个月。我已经创建了一个数据库来存储ATM卡的详细信息,我正在尝试检索它。但是,这没有发生。
我可以看到在保存卡片详细信息-“(已添加卡片)”时正在执行烤面包消息。但是,即使我打开应用程序,它也应该从数据库中获取数据。现在,它的计数为0。
我正在从主要活动中调用数据。
我也尝试通过清除应用程序数据来删除数据库。
这是我的MainActivity
。
class MainActivity : AppCompatActivity() {
companion object {
lateinit var dbHander:DBHandler
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dbHander=DBHandler(this, null, null, 1)
viewCards()
floatingActionButton.setOnClickListener {
val intent= Intent(this, NewCardRegistration::class.java)
startActivity(intent)
}
}
fun viewCards() {
val cardList= dbHander.getCards(this)
val adapter=Adapters(this, cardList)
val rv: RecyclerView= rv as RecyclerView
rv.layoutManager=LinearLayoutManager(this, LinearLayout.VERTICAL, false)
rv.adapter=adapter
}
override fun onResume() {
viewCards()
super.onResume()
}
}
还有DBHandler
class DBHandler(context: Context, name:String?, factory: SQLiteDatabase.CursorFactory?, version:Int):
SQLiteOpenHelper(context, DATABASE_NAME, factory, DATABASE_Version) {
companion object {
val DATABASE_NAME = "MyCards.db"
val DATABASE_Version = 1
val CARDS_TABLE_NAME = "CARDS"
val COLUMN_CARD_ID = "CARD_ID"
val COLUMN_BANK_NAME = "BANK_NAME"
val COLUMN_CARD_HOLDER_NAME = "NAME"
val COLUMN_CARD_VALIDITY = "CARD_VALIDITY"
val COLUMN_CARD_CVV = "CARD_CVV"
val COLUMN_CARD_NUMBER = "CARD_NUMBER"
val COLUMN_CUSTOMER_CARE_NUMBER = "CUSTOMER_CARE_NUMBER"
}
override fun onCreate(db: SQLiteDatabase?) {
val CREATE_CARD_TABLE: String = ("CREATE TABLE $CARDS_TABLE_NAME(" +
"$COLUMN_CARD_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"$COLUMN_BANK_NAME," +
"$COLUMN_CARD_CVV," +
"$COLUMN_CARD_VALIDITY," +
"$COLUMN_CARD_NUMBER" +
"$COLUMN_CUSTOMER_CARE_NUMBER" +
"$COLUMN_CARD_HOLDER_NAME)")
db?.execSQL(CREATE_CARD_TABLE)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
}
fun getCards(mCtx: Context): ArrayList<Cards> {
val query = "Select * from $CARDS_TABLE_NAME"
val db = this.readableDatabase
val cursor = db.rawQuery(query, null)
val cardsArray = ArrayList<Cards>()
if (cursor.count == 0)
Toast.makeText(mCtx, "No records Found", Toast.LENGTH_LONG).show()
else {
while (cursor.moveToNext()) {
val cards = Cards()
cards.cardID = cursor.getInt(cursor.getColumnIndex(COLUMN_CARD_ID))
cards.bankName = cursor.getString(cursor.getColumnIndex(COLUMN_BANK_NAME))
cards.cardCVV = cursor.getInt(cursor.getColumnIndex(COLUMN_CARD_CVV))
cards.cardHolderName = cursor.getString(cursor.getColumnIndex(COLUMN_CARD_HOLDER_NAME))
cards.validity = cursor.getString(cursor.getColumnIndex(COLUMN_CARD_VALIDITY))
cards.ccNumber=cursor.getDouble(cursor.getColumnIndex(COLUMN_CUSTOMER_CARE_NUMBER))
cardsArray.add(cards)
}
Toast.makeText(mCtx, "${cursor.count} Records Found", Toast.LENGTH_LONG).show()
}
cursor.close()
db.close()
return cardsArray
}
fun addCard(context: Context, cards: Cards) {
val values = ContentValues()
values.put(COLUMN_CARD_VALIDITY, cards.validity)
values.put(COLUMN_CARD_HOLDER_NAME, cards.cardHolderName)
values.put(COLUMN_CARD_CVV, cards.cardCVV)
values.put(COLUMN_BANK_NAME, cards.bankName)
values.put(COLUMN_CARD_NUMBER, cards.cardNumber)
values.put(COLUMN_CUSTOMER_CARE_NUMBER, cards.ccNumber)
val db = this.writableDatabase
try {
db.insert(CARDS_TABLE_NAME,null,values)
Toast.makeText(context, "Card Added", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
db.close()
}
}
用于保存数据的模型类是。
class Cards {
var cardID: Int=0
var bankName: String=""
var cardHolderName:String=""
var cardCVV:Int=0
var validity:String=""
var cardNumber: Double = 0.0
var ccNumber:Double=0.0
}
答案 0 :(得分:2)
val CREATE_CARD_TABLE: String = ("CREATE TABLE $CARDS_TABLE_NAME(" +
"$COLUMN_CARD_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"$COLUMN_BANK_NAME," +
"$COLUMN_CARD_CVV," +
"$COLUMN_CARD_VALIDITY," +
"$COLUMN_CARD_NUMBER" +
"$COLUMN_CUSTOMER_CARE_NUMBER" +
"$COLUMN_CARD_HOLDER_NAME)")
您在此处的列之间缺少几个,
逗号。结果,该表没有您认为有的列。
try {
db.insert(CARDS_TABLE_NAME,null,values)
Toast.makeText(context, "Card Added", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
insert()
不会引发错误。它返回-1表示错误,并且如果您使用insertOrThrow()
,则会得到一个异常,指出“未知列”。
如何修复:
onCreate()
SQL并添加缺少的逗号insert()
更改为insertOrThrow()
,以便您的面包在这里按预期工作onCreate()
。