【问题标题】:Create table and insert records in table in Android using RubyMotion使用 RubyMotion 在 Android 中创建表并在表中插入记录
【发布时间】: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


【解决方案1】:

这是 RubyMotion Android 的一个已知限制。您不能从 Ruby 执行 super 或覆盖 Java 类构造函数。看这个链接http://www.rubymotion.com/developers/guides/manuals/android/project-management/#_extending_classes_with_java

此外,我正在为此做一些事情,这样您就不必处理原始 SQL。如果您愿意(或需要),您可以。无论如何,我会在我的宝石中支持这一点。当我发布它时,我会编辑我的答案。谢谢。还没准备好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多