0

I am trying to build an Android Xamarin Application. I have created an SQLite database named regjistri.sqlite . The database it's not corrupted, I can open it via SQLite browser on my PC. This database I have save it to my android Documents folder. I am trying to access it like below via sqlite-net-pcl library.

string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "regjistri.sqlite");
var db = new SQLiteConnection(dbPath);
var data = db.Query<object>("SELECT COUNT(*) FROM tblLidhja ");

Also I have gice the application the reading and writing permissions. When I debug it via USB cable, it give me the "SQLite.SQLiteException: no such table: tblLidhja" exception. Can someone help me whats may be wrong, or what should I change to read some data?

Adnand
  • 562
  • 1
  • 8
  • 25
  • You mean instead of **var data = db.Query("SELECT COUNT(*) FROM tblLidhja ");** it should be **var data = db.Query("SELECT COUNT(*) FROM tblLidhja ");** ? – Adnand Aug 25 '19 at 19:44
  • 1
    What is your actual `tblLidhja` class code and where is the `TableCreate` call in your code? – SushiHangover Aug 25 '19 at 20:05
  • if the path does not exist SQLite will create an empty db for you - this is what I suspect is happening - your path is not correct. Use File.Exists to verify – Jason Aug 25 '19 at 21:16
  • You have to call `db.CreateTable()` after the connection to DB is opened and before querying any data from it (that's what the exception is telling you - there is no such table in your DB). You can find an example at sqlite-net repo page: https://github.com/praeclarum/sqlite-net – Vitalii Ilchenko Aug 26 '19 at 13:39

1 Answers1

0

Before getting data , first need to check whether this table is exist in data base .You can refer to this document to check .

By the way ,having a try with this way to get count from database:

var db = new SQLiteConnection("DbPath");
var stocksStartingWithA = db.Query<TableName>("SELECT * FROM DbNmae ");
int count = stocksStartingWithA.Count; //get count from table

Here is similar discussion with java code, but it also can be refer in xamrin project.

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30