简体   繁体   中英

sqlite cannot open database file in UPW with windows IOT on a Raspberry pi

I'm trying to create a Database on my raspberry pi that the UWP can use to read and Write into. but i always get the error "SQLite Error 14: 'unable to open database file" and i cant seem to fix it. I've tried looking for fixes but everything seems to be catered for sqlite.net framework were as i'm using the Microsoft.data.sqlite framework. i've tried to use the sqlite.net framework but i cant find the documentation for all the methods.

   using (SqliteConnection db =
                new SqliteConnection("Filename = sqliteSPSystem.db"))
            //"Filename=sqliteSPSystem.db"
            {
                db.Open();

                String tableCommand = "CREATE TABLE IF NOT " +
                    "EXISTS ParkingData (Primary_Key INTEGER PRIMARY KEY, " +
                    "TimeIN NVARCHAR(2048) NULL,TimeOUT NVARCHAR(2048) NULL)";

                SqliteCommand createTable = new SqliteCommand(tableCommand, db);

                createTable.ExecuteReader();
            }

it would be really nice to get past this because i'v been struggling for about 2 weeks now and its getting old.

You need to check if the database file existed int the folder, if it does not exist, you can create the db file like following.

public async static void InitializeDatabase()
{ 
     await ApplicationData.Current.LocalFolder.CreateFileAsync("sqliteSPSystem.db", CreationCollisionOption.OpenIfExists);
     string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sqliteSPSystem.db");
     using (SqliteConnection db =
        new SqliteConnection($"Filename={dbpath}"))
    {
        db.Open();

        String tableCommand = "CREATE TABLE IF NOT " +
                "EXISTS ParkingData (Primary_Key INTEGER PRIMARY KEY, " +
                "TimeIN NVARCHAR(2048) NULL,TimeOUT NVARCHAR(2048) NULL)";

        SqliteCommand createTable = new SqliteCommand(tableCommand, db);

        createTable.ExecuteReader();
    }
}

Thanks again. This worked and i can finnaly go on with the project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM