【问题标题】:Access specific column data with Android Xamarin SQLite使用 Android Xamarin SQLite 访问特定列数据
【发布时间】:2020-10-20 16:25:39
【问题描述】:

我正在 Xamarin Android 中构建登录活动,并且我已插入所有模块(库和文件)并准备就绪,但我在从表中检索单个对象(实际上是电子邮件)时遇到问题(它已经包含数据)...我的代码解释如下...

//The class that acts as a blueprint to the table is this one
class Person {
       [PrimaryKey, AutoIncrement]
        public int id { set; get; }
        public string name { set; get;}
        public string department { set; get; }
        public  string email { set; get; }
        public string password { set; get; }
            }

处理登录逻辑的文件

class Login : AppCompatActivity
    {
     protected override void OnCreate(Bundle savedInstanceState){
    //Login button definition
  Button login=(Button)FindViewById<Button>(Resource.Id.login);
     //Make the button raise login event on click
    login.Click +=Login;                                                                }
     }
//Button method for login
private void Login(object sender, EventArgs e){
   //Define location that the connection will use to create and store tables
string dbpath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "dbpersons.db");
 //Define new connection to database using a connection string
  var conn=new SQLite.SQLiteConnection(dbpath);
    try{
          //Microsoft manual says this can access elements of a row using id parameter
        var stock = db.Get<Person>(5);
      // I need code that will let me access email from specific column in table
   }catch(SQLite.SQLiteException m){
    //Display exception in a toast
     Toast.MakeText(Application.Context,m.Message,ToastLength.Long).Show();
    }  
  }
}

我需要该电子邮件与用户将在 ``EditText``` 中输入的另一个值进行比较

【问题讨论】:

  • Stock 表是否有 email 列?您发布的课程定义是针对Person,而不是Stock
  • @Jason,编辑中好点
  • 现在很好,我只想要代码,我将使用它从表中选择电子邮件并将其与 EditText 中输入的值进行比较

标签: android sqlite xamarin


【解决方案1】:

您正在从数据库中检索一个对象,因此要访问该对象的属性只需使用该属性

var stock = db.Get<Person>(5);
// I need code that will let me access email from specific column in table
var email = stock.email;

【讨论】:

  • 感谢这有助于从与该记录关联的 id 检索电子邮件,您可以编辑以与在 EditText 中输入的值用户进行比较吗?如果 true 启动一个新活动(仪表板活动)并将电子邮件作为意图附加信息传递给仪表板类
  • 如何检查 SQLite 查询是否返回 true
  • Get 返回一个对象,而不是布尔值。
  • 我设法找到了这段代码来获取一个对象,sql 可能会返回 null,但我已经处理好了,然后我在 if else 逻辑及其 object mail=connection.FindByQuery&lt;Table&gt;(query+parameters); 中有代码
  • 如果您还有其他问题,您应该发布一个新问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
相关资源
最近更新 更多