How to use the SQLite database with the Windows 7 phone?

advertisements

I am developing window phone 7 application. I am new to the window phone 7 application. I am new to the Application.GetResourceStream() method. I am using the following link in my application.

http://wirebear.com/blog/2010/11/12/using-sqlite-in-your-wp7-app

I am following the steps described in the above link. I am facing problem at Application.GetResourceStream() method. In the above method I am using the following code

Stream src = Application.GetResourceStream(
                 new Uri(@"/SQLiteConnectivity;component/" + dbName,
                     UriKind.Relative)).Stream;

I have right clicked my project name & selected the properties & found the assembly name as 'SQLiteConnectivity'. After running the application I am getting the null reference exception error at Application.GetResourceStream(). I am also unaware of what should I use instead of 'component' in Application.GetResourceStream() method. Can you please provide me any code or link or any solution through which I can resolve the above issue ?

Yes I got the answer as described in the answer section. I solved the above issue by setting the 'Build Action' in properties of my database as 'Resource' & 'Copy to Output Directory' as 'Copy always'. But I am now facing the new problem

I have declared the following code in my app.xaml.cs

public partial class App : Application
{
    public PhoneApplicationFrame RootFrame { get; private set; }

    private DBHelper _db;
    public DBHelper db
    {
        get
        {
            if (_db == null)
                _db = new DBHelper("ExpenseManager.db");
            return _db;
        }
    }
}

and calling it in my mainpage.xaml.cs as follows

private void button1_Click(object sender, RoutedEventArgs e)
{
      // Code runs "for real"

    _customerEntries = (Application.Current as App).db.SelectObservableCollection<Category>("SELECT  Category_Name FROM Category WHERE Category_ID=1");
    textBox1.Text = _customerEntries.ToString();
}

I have added one class in below the the mainpage class as follows

public class Category
{
    public int Category_ID { get; set; }
    public string Category_Name { get; set; }
}

Now when I run my application I get the error on button click as 'file is encrypted or is not in a database' in the following function of SQLClient.cs at BindAll(ppStmt);

Sqlite3.Vdbe Prepare()
{
    Sqlite3.Vdbe ppStmt=new Sqlite3.Vdbe();
    if (Sqlite3.sqlite3_prepare_v2(_db, CommandText, CommandText.Length, ref ppStmt, 0) != Sqlite3.SQLITE_OK)
        throw new SQLiteException(Sqlite3.sqlite3_errmsg(_db));
    BindAll(ppStmt);
    return ppStmt;
}

Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.


You will want to verify your Database file's build property is set to resource and you will want to check the value of the dbName property to ensure it is the full file's name including any folder structure.

The db property of the App needs to be setup differently. In your App.xaml.cs file please change this line:

public DBHelper db = new DBHelper("DATABASE.s3db");

to this:

private DBHelper _db;
public DBHelper db
{
  get
  {
    if (_db == null)
      _db = new DBHelper("DATABASE.s3db");
    return _db;
  }
}

Be sure to replace the DATABASE.s3db with the name of your database file including the folder structure.

For the error:

'file is encrypted or is not in a database'

What version of SQLite did you create this database in? You must be using SQLite version 3. If you are using SQLite3, do you have a password set on this database?