简体   繁体   中英

Swedish characters from javascript to mssql - appear as question marks in db table?

So I have this weird issue with node js right now and can not find any solution...

Im trying to insert values to my mssql table and some of the values includes the swedish characters "åäö". Anyhow when I do this they appear as "??????". Each special character appear as two question marks ("ö" -> "??").

Some details:

*Package I'm using is in js: msnodesqlv8

var sql = require('msnodesqlv8');
const connectionString = "server=host;Database=MyDb;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
q = "Insert into [MyDb].[dbo].[MyTable] (testCol) values ('ö')"

sql.query(connectionString, q, (err,data) => {
      if (err) {
           console.log(err);
      }
      else {
           console.log('Imported row to table.');
      }
})

*The columns in the db table that will retrieve values containing "åäö" is defined as datatype nvarchar(50)

  • I have also trid with N'ö' in variable q and these characters (within the double quotes) appear in db table "ᅢᄊ"

*console.log('ö') prints ö in console as expected

*I have tried all conversions i can think about in js (for example utf-8, latin1, etc...)

*Collation in db is Finnish_Swedish_CI_AS

*I have tried to read out swedish chrs from another table and it works fine through the package msnodesqlv8

The problem to me seems to be somewhere when sending the query from js (everything looks fine here), through package msnodesqlv8 and when mssql shall interpret the values. Im very glad if someone can help me.

Is there some reason you're not using a prepared statement? It takes care of the encoding for you, eg:

const sql = require('msnodesqlv8')
const connectionString = "server=YourServerIp,YourServerPort;Database=StackOverflow;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0};"
console.log('Connecting...')
sql.open(connectionString, (err, conn) => {
    console.log('Dropping...')
    conn.query('drop table if exists [dbo].[Swedish]', (err,) => {
        console.log('Creating...')
        conn.query('create table [dbo].[Swedish] (testCol nvarchar(50))', (err) => {
            console.log('Inserting...')
            conn.prepare('insert [dbo].[Swedish] (testCol) values (?)', (err, ps) => {
                ps.preparedQuery(['ö'], (err) => {
                    ps.free();
                    console.log('Selecting...')
                    conn.query('select * from [dbo].[Swedish]', (err, rows) => {
                        console.log(`${JSON.stringify(rows)}`)
                    })
                })
            })
        })
    })
})

Which yields...

> node .\swedish.js        
Connecting...
Dropping...
Creating...
Inserting...
Selecting...
[{"testCol":"ö"}]

The same character is present when queried via SSMS.

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