【发布时间】:2015-01-29 07:44:56
【问题描述】:
我正在创建数据库类并在单击按钮时创建该类的对象,但控制器没有点击该类这是我的数据库代码
class DataStore < Android::Database::SQLite::SQLiteOpenHelper
DATABASE_NAME = "MyFriendsDatabase"
def initialize(context)
puts "consructor in database"
#super(context, Name, Factory, Version)
#context - this is used to create database
#name - name of the database file
#factory - A cursor factory(usually pass nil)
#Version - the version of database. this number is used to identify if there is an upgrade or downgrade of the database
super(context, DATABASE_NAME, nil, 1)
end
def onUpgrade(db, oldVersion, newVersion)
#db.execSQL("DROP TABLE IF EXISTS credentials")
onCreate(db)
end
#create
def onCreate(database)
puts "table creation"
database.execSQL("CREATE TABLE credentials(username TEXT, password TEXT)")
end
#insert
def add_credentials(username, password)
puts "inserting data in table"
values = ContenValues.new(2)
values.put("username", username)
values.put("password", password)
getWritableDatabase.insert("credentials", "username", values)
end
#retrieve
def get_credentials
puts "coming here &&&&&&&&&&&"
cursor = getReadableDatabase.rawQuery("select * from credentials", nil)
cursor
end
#deleting all records
def deleteAll
getWritable.delete("credentials", nil, nil)
end
end
点击按钮时执行的代码是
database = DataStore.new(self)
database.add_credentials(@text_box_value.getText.toString, @text_box.getText.toString)
puts database.get_credentials
任何人都可以帮助我。
提前致谢
【问题讨论】:
-
如果你编辑你的答案来修复那些代码块,它会有所帮助。
标签: sqlite insert rubymotion oncreate