首先,我运行下面的代码行以获取以下收益:(显示图像仅供参考)
var query_user1 = await _connection.QueryAsync<FavoritesProfile>("SELECT * FROM FavoritesProfile");
因此,我显然在我的数据表中将“ UsersProfileProfile”加载了User =“ Shane”和Titles =“ The Avengers”值。但是,当我尝试下一行代码时,会出现异常。
var query_user = await _connection.QueryAsync<FavoritesProfile>("SELECT FROM FavoritesProfile WHERE User LIKE '%" + user + "%' AND Titles LIKE '%" + _movie.Title + "%'");
我确保用户是一个字符串,_movie.Title也是一个字符串。它们也分别对应于Shane和The Avengers。我还将添加该类中的所有代码,以防万一问题出在其他地方。如果您有任何见解,将不胜感激。我知道我不是以最干净的方式实现SQLite数据库。
using Xamarin.Forms;
using System;
using MovieApp.Services;
using MovieApp.Models;
using SQLite;
using MovieApp.Persistance;
namespace MovieApp.Views
{
public partial class MovieDetailsPage : ContentPage
{
private MovieService _movieService = new MovieService();
private Movie _movie;
private SQLiteAsyncConnection _connection;
private string user = MainPage._username; //simplicity purposes as we didn't implement fully functions login page
public class FavoritesProfile
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(255)]
public string User { get; set; }
[MaxLength(255)]
public string Titles { get; set; }
}
public MovieDetailsPage(Movie movie)
{
if (movie == null)
throw new ArgumentNullException(nameof(movie));
_movie = movie;
InitializeComponent();
_connection = DependencyService.Get<ISQLiteDb>().GetConnection();
}
protected override async void OnAppearing()
{
var query_user1 = await _connection.QueryAsync<FavoritesProfile>("SELECT * FROM FavoritesProfile");
//will create table if it does not already exist
await _connection.CreateTableAsync<FavoritesProfile>();
//BindingContext = await _movieService.GetMovie(_movie.Title);
BindingContext = _movie;
CheckForFavoriteAsync();
base.OnAppearing();
}
private async void CheckForFavoriteAsync()
{
try
{
var query_user = await _connection.QueryAsync<FavoritesProfile>("SELECT FROM FavoritesProfile WHERE User LIKE '%" + user + "%' AND Titles LIKE '%" + _movie.Title + "%'");
if (query_user.Count >= 1)
favoriteicon.Icon = "star_filled.png";
} catch (Exception) {
}
}
public async void Favorite_Clicked(object sender, EventArgs e)
{
if(favoriteicon.Icon == "star_filled.png")
{
//unadd from favorites for user
try
{
var query_user = await _connection.QueryAsync<FavoritesProfile>("DELETE FROM FavoritesProfile WHERE User LIKE '%" + user + "%' AND Titles LIKE '%" + _movie.Title + "%'");
} catch (Exception)
{
}
favoriteicon.Icon = "star_unfilled.png";
}
else
{
var query_user2 = await _connection.QueryAsync<FavoritesProfile>("SELECT * FROM FavoritesProfile");
//add to favorites for user
try
{
var query_user = await _connection.QueryAsync<FavoritesProfile>("SELECT FROM FavoritesProfile WHERE User LIKE '%" + user + "%' AND Titles LIKE '%" + _movie.Title + "%'");
} catch (Exception)
{
var favorites = new FavoritesProfile { User = user, Titles = _movie.Title };
await _connection.InsertAsync(favorites);
}
favoriteicon.Icon = "star_filled.png";
}
}
}
}
总体而言,我的Delete查询和通用的Select *查询一样有效,但是我无法过滤另一个查询并将结果返回给我。我只想在已有用户名和movieTitle条目的情况下得到结果,以便我可以填写图标向用户显示他们已经将该电影添加到收藏夹中。
答案 0 :(得分:1)
第二个查询的SELECT语句无效。您需要指定要选择的列。
此行:
var query_user = await _connection.QueryAsync<FavoritesProfile>("SELECT FROM FavoritesProfile WHERE User LIKE '%" + user + "%' AND Titles LIKE '%" + _movie.Title + "%'");
可能应该是
var query_user = await _connection.QueryAsync<FavoritesProfile>("SELECT * FROM FavoritesProfile WHERE User LIKE '%" + user + "%' AND Titles LIKE '%" + _movie.Title + "%'");