简体   繁体   中英

How can I store a dictionary in an Azure SQL Database? (Swift)

I want to store a dictionary in a column in an Azure SQL Database. I've tried to store a dictionary by doing :

let item = ["text":["foo":"bar"]]
    let itemTable = client.tableWithName("TodoItem")
    itemTable.insert(item) {
        (insertedItem, error) in
        if (error != nil) {
            print("Error" + error!.description);
        } else {
            print("Item: " String(insertedItem!))
        }
    }

But when I query the database I get

Item: [Object object]

How can I store and query dictionaries in the database without the items changing?

I think you could convert dictionary to json string before inserting into azure sql database and convert json string to dictionary after you retrieved from database.

The advantage is that you could store dictionary with basic string types and no need to worry about if there are any potential issue with dictionary type.

The only one more step is convertion between dictionary and json string, hopefully swift has provided methods to handle this. Check out Convert Dictionary to JSON in Swift for more details and hope this do help.

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