最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I add charset to Knex.js connection string? - Stack Overflow

programmeradmin3浏览0评论

I am using Knex.js for database connections. I would like to use a connection string instead of the connection object for connecting to a database and be able to specify the charset.

Example:

connection: 'mysql://root:[email protected]:3306/test-db'

NOT

connection: {
    host     : '127.0.0.1',
    user     : 'root',
    password : 'secret',
    database : 'test-db',
    charset  : 'utf8'
}

I have the connection string working.

Could someone explain how I would specify the charset in the Knex.js connection string?

I am using Knex.js for database connections. I would like to use a connection string instead of the connection object for connecting to a database and be able to specify the charset.

Example:

connection: 'mysql://root:[email protected]:3306/test-db'

NOT

connection: {
    host     : '127.0.0.1',
    user     : 'root',
    password : 'secret',
    database : 'test-db',
    charset  : 'utf8'
}

I have the connection string working.

Could someone explain how I would specify the charset in the Knex.js connection string?

Share Improve this question edited Jul 26, 2016 at 5:20 slebetman 114k19 gold badges146 silver badges178 bronze badges asked Jul 26, 2016 at 4:45 Kevin BakerKevin Baker 1,1973 gold badges12 silver badges25 bronze badges 3
  • 1 Gadzooks!, yet another 3rd party package! If you can't figure it out, execute SET NAMES utf8 right after connecting; that's usually a valid fallback. – Rick James Commented Jul 27, 2016 at 0:54
  • The knex docs says that it passes the connection string directly to the underlying driver: knexjs/#Installation-client . If so, the underlying driver might not be showing the expected behavior: github./mysqljs/mysql#establishing-connections . I'm just guessing, but it may worth the try. – Sombriks Commented Jul 27, 2016 at 6:08
  • Thanks, this is very helpful. The docs show the below connection string which looks promising: connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700'); – Kevin Baker Commented Jul 27, 2016 at 7:32
Add a ment  | 

1 Answer 1

Reset to default 6

Try:

connection: 'mysql://root:[email protected]:3306/test-db?charset=utf8mb4'

See connection string options as described here: https://github./mysqljs/mysql#connection-options

发布评论

评论列表(0)

  1. 暂无评论