使用RubyMotion在Android中创建表并在表中插入记录

时间:2015-01-29 07:44:57

标签: sqlite insert rubymotion oncreate

我正在创建数据库类并在按钮单击时创建该类的对象,但是控制器没有按此类命令这里是我的数据库代码

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

任何人都可以帮助我。

提前致谢

1 个答案:

答案 0 :(得分:0)

这是RubyMotion Android的一个已知限制。你不能从Ruby做超级或覆盖Java类构造函数。请参阅此链接http://www.rubymotion.com/developers/guides/manuals/android/project-management/#_extending_classes_with_java

此外,我正在为此做点什么,所以你不必处理原始SQL。如果你想要(或需要),你可以。无论如何,我会在我的宝石中支持这个。当我发布它时,我会编辑我的答案。谢谢。它尚未准备好。