2014-05-07 14 views
5

Ho cercato di creare un telefono Windows e mi piacerebbe usare SQLite per memorizzare entrambi i miei dati e imparare come usarlo sulle app di Windows Phone. Per questo scopo sto usando "SQLite.Net-PCL", ma continuo a ricevere un'eccezione di file non trovata. Questo il codice che ho scritto:Connessione SQLite.Net-PCL non trovando il DB

 String ConnectionString = Path.Combine(ApplicationData.Current.LocalFolder.Path, Connection); 
     if (File.Exists(ConnectionString)) 
     { 
      SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 e = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8(); 
      Con = new SQLiteConnection(e,ConnectionString); 
     } 

     else { 
      SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8 e = new SQLite.Net.Platform.WindowsPhone8.SQLitePlatformWP8(); 
      File.Create(ConnectionString); 
      Con = new SQLiteConnection(e, ConnectionString);    
     } 

ho pensato che forse ottengo questo errore perché creo manualmente un file vuoto, ma se questo è il problema, come posso creare un DB nel caso in cui non esiste alcun database nel telefono ?

+0

Qual è il valore di ConnectionString, non è ovvio dal codice? – Gavin

risposta

0

Non è necessario creare il file da soli, poiché il costruttore SQLiteConnection lo gestisce per te.

public SQLiteConnection(ISQLitePlatform sqlitePlatform, string databasePath, bool storeDateTimeAsTicks = false, IBlobSerializer serializer = null) 
    : this(
     sqlitePlatform, databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks, serializer) 
{ 
} 

Per questo si deve solo aprire la connessione, creare le tabelle e che dovrebbe essere così.

class ExampleDataContext 
{ 
    public const string DATABASE_NAME = "data.sqlite"; 
    private SQLiteConnection connection; 

    public TableQuery<Foo> FooTable { get; private set; } 
    public TableQuery<Bar> BarTable { get; private set; } 

    public ExampleDataContext() 
    { 
     connection = new SQLiteConnection(new SQLitePlatformWinRT(), Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, DATABASE_NAME)); 

     Initialize(); 

     FooTable  = connection.Table<Foo>(); 
     BarTable  = connection.Table<Bar>(); 
    } 

    private void Initialize() 
    { 
     connection.CreateTable<Foo>(); 
     connection.CreateTable<Bar>(); 
    } 
} 

Non preoccuparti che Initialize, le tabelle vengono creati solo quando sono non c'è ancora.

+0

SQLitePlatformWinRT() supporta WP8.1? Se sì, dov'è? Se no, dov'è la piattaforma per WP8.1? –

+1

@JohnCroneh Penso che non sia stato incluso di default al momento della scrittura. Ho scaricato la fonte e aggiunto manualmente il target di build. –

+0

Grazie per la risposta. Dov'è esattamente la fonte? –

Problemi correlati