我正在开发Xamarin.Android中的Android应用程序。我创建了一个Sqlite数据库,我可以在其中添加数据并获取所有数据。但现在我想从表中检索单个行值,分别对应于列键。
以下是我的AlbumTable
代码:
namespace Myapp
{
class AlbumTable
{
public long SR_ID { get; set; }
public string a{ get; set; }
public string FileName { get; set; }
public string OrderId { get; set; }
public string Mobile { get; set; }
public string Date { get; set; }
public string CreatedOn { get; set; }
public string UpdatedOn { get; set; }
}
}
这是数据库中表的所有列名,我存储在数据库中。
这是DatabaseHelper文件代码:
namespace Myapp
{
class DatabaseHelper
{
Java.IO.File dir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Android/data/com.abhijit.testing.app/databases");
public bool createDataBase()
{
try
{
using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
{
connection.CreateTable<AlbumTable>();
return true;
}
}
catch (SQLiteException e)
{
return false;
}
}
public bool InsertIntoTable(AlbumTable album)
{
try
{
System.Console.Write("Data Saved Successfully");
using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
{
connection.Insert(album);
return true;
}
}
catch (SQLiteException e)
{
return false;
}
}
public List<AlbumTable> getalldata()
{
try
{
using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
{
return connection.Table<AlbumTable>().ToList();
}
}
catch (SQLiteException ex)
{
return null;
}
}
}
}
现在我想根据orderid
字段从表中获取一条记录。
我想检索一行与orderid
匹配的行。
我是Xamarin.Android的新手,所以我对如何做不了解。
答案 0 :(得分:0)
您只需要使用相同的getalldata代码,并使用Where(condition)子句和SingleOrDefault()更改.ToList()以检索1行或null。
&#xA;&# xA; public AlbumTable getRow(string orderId)&#xA; {&#xA;&#xA;尝试&#XA; {&#XA; using(var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath,“album.db”)))&#xA; {&#XA;&#XA; return connection.Table&lt; AlbumTable&gt;()。Where(x =&gt; x.OrderId == orderId).SingleOrDefault();&#xA; }&#XA;&#XA; }&#XA; catch(SQLiteException ex)&#xA; {&#XA; return null;&#xA; }&#XA;}&#XA; 代码>
&#XA;
答案 1 :(得分:0)
如果我没错,你正在寻找这样的东西:
for(int i = 0; i<5; i++){
// LINEAR LAYOUT
LinearLayout layoutChild = new LinearLayout(context);
layoutChild.setOrientation(LinearLayout.HORIZONTAL);
// Linear Layout - ID
layoutChild.setId(i);
// Linear Layout - Params
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayoutCompat.LayoutParams.MATCH_PARENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT
);
lp.setMargins(0,20,0,20);
layoutChild.setLayoutParams(lp);
layoutChild.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
// TEXT VIEW - Label
final TextView textViewLabel = new TextView(context);
textViewLabel.setId(i);
textViewLabel.setLayoutParams(new LinearLayout.LayoutParams(300, LinearLayout.LayoutParams.WRAP_CONTENT));
textViewLabel.setTextSize(20);
textViewLabel.setPadding(10, 10, 10, 10);
textViewLabel.setText(descr);
// SWITCH - flag
Switch switch_flag = new Switch(context);
switch_flag.setId(i);
// TEXT VIEW - SeekBar Value
final TextView textViewSeekValue = new TextView(context);
textViewSeekValue.setId(i);
textViewSeekValue.setLayoutParams(new LinearLayout.LayoutParams(40, LinearLayout.LayoutParams.WRAP_CONTENT));
textViewSeekValue.setTextSize(15);
textViewSeekValue.setPadding(10, 10, 10, 10);
// SEEK BAR - slider
final SeekBar seekBar_slider = new SeekBar(context);
seekBar_slider.setId(i);
seekBar_slider.setMax(100);
LinearLayout.LayoutParams lp_seekbar = new LinearLayout.LayoutParams(200, LinearLayout.LayoutParams.MATCH_PARENT);
seekBar_slider.setLayoutParams(lp_seekbar);
seekBar_slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0) {}
public void onStartTrackingTouch(SeekBar arg0) {}
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
//textViewSeekValue.setText(arg1); // error
//error
TextView textView = new TextView(context);
textView = (TextView) getView().findViewById(textViewSeekValue.getId());
textView.setText(arg1);
}
});
LinearLayout.LayoutParams lp_ImageView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp_ImageView.gravity=Gravity.CENTER;
imageViewSem.setLayoutParams(lp_ImageView);
layoutChild.addView(textViewLabel);
parentLayout.addView(layoutChild);
此代码将获取上述变量中特定列的所有数据,祝你好运。
并且为了获得列数据,您可以使用foreach循环。
答案 2 :(得分:0)
1.您可以直接使用FirstOrDefault()Linq方法: -
使用System.Linq; 命名空间添加。
public AlbumTable GetRow(string orderId) {
try
{
using (var connection = new
SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
{
return connection.Table<AlbumTable>().FirstOrDefault(x => x.OrderId
== orderId);
}
}
catch (SQLiteException ex)
{
return null;
}
}
它将直接返回单个记录。