我是Ror newbe,我正在尝试使用rails创建我的第一个webapp.我在安装gem mysql2时遇到问题:这是我的实际配置:
ruby -v:ruby 1.9.3p194(2012-04-20修订版35410)[x86_64-linux]
rails -v:Rails 3.2.3
gem -v:1.8.24
michele@michele:~/rails/my_app$ gem install mysql2
Building native extensions. This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed
Installing ri documentation for mysql2-0.3.11...
Installing RDoc documentation for mysql2-0.3.11...
Run Code Online (Sandbox Code Playgroud)
在此之后我编辑了database.yml并运行:
rake db:create
它返回:
rake aborted!
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.)
Run Code Online (Sandbox Code Playgroud)
所以,当我最终尝试安装它时,我收到错误:
michele@michele:~/rails/my_app$ gem install activerecord-mysql2-adapter
ERROR: Could not find a valid gem 'activerecord-mysql2-adapter' (>= 0) in …Run Code Online (Sandbox Code Playgroud) 我的堆栈如下:
已安装MySQL 5.5(按照http://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/中的步骤进行操作,因为yum仍然具有MySQL 5.1和如果我尝试yum安装任何与mysql相关的软件包,它当然会因为yum所期望的而破坏5.1)
$> mysql --version mysql Ver 14.14使用readline 5.1为Linux(x86_64)分发5.5.27
$> ruby --version ruby 1.9.3p194(2012-04-20修订版35410)[x86_64-linux]
$> rvm --version rvm 1.14.6(稳定)作者:Wayne E. Seguin,Michal Papis [https://rvm.io/]
$> uname -a Linux localhost.mkf 2.6.32-279.5.1.el6.x86_64#1 SMP Tue Tue Aug 14 23:54:45 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
$> nginx -v nginx版本:nginx/1.2.2
$> rails -v Rails 3.2.6
当我尝试安装mysql2 gem时,出现以下错误:
$> gem install mysql2
Building native extensions. This could take a while...
ERROR: Error installing mysql2:
ERROR: Failed to build gem native …Run Code Online (Sandbox Code Playgroud) 运行软件包安装后,出现此错误。我试过编辑/ etc / paths文件,但仍然没有用。
Gem :: Ext :: BuildError:错误:无法构建gem本机扩展。
/Users/adam/.rvm/rubies/ruby-2.1.5/bin/ruby -r ./siteconf20150224-4249-ltvvu1.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /Applications/MAMP/Library/bin/mysql_config
-----
checking for mysql.h... no
checking for mysql/mysql.h... yes
checking for errmsg.h... no
-----
errmsg.h is missing. please check your installation of mysql and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due …Run Code Online (Sandbox Code Playgroud) 使用 mysql2/promise 模块,我创建一个像这样的连接池:
import mysql from 'mysql2/promise';
async function getConnection() {
let pool = await mysql.createPool({
connectionLimit: 10,
host:'localhost',
user: 'root',
database: 'customers'
});
return pool.getConnection();
}
export {
getConnection
};
Run Code Online (Sandbox Code Playgroud)
在另一个文件中,我可以像这样访问此功能:
import {getConnection} from '../config/database/mysql';
async function getCustomerAddresses(id){
let conn = await getConnection();
let [rows, fields] = await conn.execute(
`SELECT *
FROM addresses WHERE customer = ?`, [id]);
conn.release();
return rows;
}
Run Code Online (Sandbox Code Playgroud)
调用该函数几次后,我收到以下错误:
太多联系
我究竟做错了什么?有没有一种更优雅的方法,这样我就不必在每个函数中都包含 getConnection() 调用?
我正在尝试将一个 JSON 对象插入到 MySQL JSON 列中,我得到: Invalid JSON text: "Invalid escape character in string."
有问题的字符是\n对象的这一部分:
"summary":"Summary of Qualifications:\nAccomplished and results-driven General Business Director with a consistent track record."
我尝试过 JSON.stringify、JSON_OBJECT、JSON_QUOTE,最近的尝试包括:
summary.replace("\n", "\\n")以及用适当的替换现有转义字符的变化?转义字符。
如果可能有任何影响,我正在使用 Node、Express 和 JawsDB。
编辑:这是实际的插入代码:
const fields = '(myField)';
const values = `('${JSON.stringify(field.contents || {})}')`;
db.query('INSERT INTO mydb ' + fields + ' VALUES ' + values,
err => {
if (err) {
console.log(values);
throw(err) ;
}
}
);
Run Code Online (Sandbox Code Playgroud) 最近我想学习 Node.js 来帮助我找到一份工作,所以我开始了一个网页抓取应用程序。
我从 mysql 包开始,但在编写代码后我没有想到这是一个异步过程。
然后我发现 mysql2 有承诺,但我不确定我是否理解如何正确使用它们,而且我的做法很糟糕。
这是我的代码
const mysql = require('mysql2');
const pool = mysql.createPool({ ... });
var categorias = [];
var querySQL;
/*
Here goes web scraping stuff not needed in this question
*/
pool.getConnection(function(err, connection){
if(err) throw err;
querySQL = "SELECT 1 FROM Categories LIMIT 1";
connection.promise().query(querySQL).then(([rows,fields])=> {
if (rows!=undefined) {
console.log("The table already exist");
}else {
querySQL = "CREATE TABLE Categories (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20))";
connection.query(querySQL,function(err,rows,field){
if(err) throw err;
console.log("The table …Run Code Online (Sandbox Code Playgroud) 运行集成测试时出现错误:
0 passing (17s)
1 failure
1) Registration page
register new users allowed and update status in the database:
TypeError: Net.connect is not a function
at new Connection (webpack:///./node_modules/mysql2/lib/connection.js:50:0)
at ./node_modules/mysql2/index.js.exports.createConnection (webpack:///./node_modules/mysql2/index.js:10:0)
at Context.eval (webpack:///./cypress/integration/registration.spec.js:23:34)
Run Code Online (Sandbox Code Playgroud)
这是我的环境:
MySQL Workbench
MySQL Server 8.0.29
Run Code Online (Sandbox Code Playgroud)
我提出了本地后端,我可以访问数据库。这是我的代码:
const mysql2 = require('mysql2');
describe('Registration page', () => {
beforeEach(() => {
// visit the registration page
cy.visit('http://localhost:3000/registration');
});
it('register new users allowed and update status in the database', () => {
// fill out the registration form …Run Code Online (Sandbox Code Playgroud) 我在Mac OS X Lion环境中使用RVM安装了Ruby和Rails.
我使用mysql2 gem(使用MAMP库而不是mysql安装)使Rails正常工作.一切都很好......
直到我尝试编写一个独立的ruby脚本(在Rails上下文中).而现在我无法让mysql2 gem工作.
从脚本内部或Ruby命令行(irb),
require "mysql2"
Run Code Online (Sandbox Code Playgroud)
指令导致以下错误:
1.9.2p290 :001 > require "mysql2"
dyld: lazy symbol binding failed: Symbol not found: _mysql_get_client_info
Referenced from: /Users/mick/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
Expected in: flat namespace
dyld: Symbol not found: _mysql_get_client_info
Referenced from: /Users/mick/.rvm/gems/ruby-1.9.2-p290/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
Expected in: flat namespace
Run Code Online (Sandbox Code Playgroud)
我尝试过几种东西:
export DYLD_LIBRARY_PATH=/Applications/MAMP/Library/lib/mysql/:$DYLD_LIBRARY_PATH
运用 sudo ln -s /Applications/MAMP/Library/lib/mysql/libmysqlclient.16.dylib /usr/lib/libmysqlclient.16.dylib
没有任何效果.我想我很快就会哭...
我试图弄清楚如何使用XAMPP安装的MySQL获得OS X Lion以使用rails中的mysql2 gem.
这是我尝试过的:
这是我正在运行以尝试安装它的命令:
gem install mysql2 -- --with-mysql-dir=/var/mysql --with-mysql-include=
/var/mysql/include --with-mysql-lib=/var/mysql/lib --with-mysql-config=
/Applications/XAMPP/xamppfiles/bin/mysql_config
Run Code Online (Sandbox Code Playgroud)
我不知道如何让它发挥作用.我唯一能想到的是,我不知何故没有正确指向目录.我试过从with-mysql-include,with-mysql-dir和with-mysql-lib配置选项中删除/ include /,/ mysql /和/ lib /.没有运气.我已经尝试将它们指向XAMPP安装.还没有运气.
返回的错误是:
ERROR: Error installing mysql2:
ERROR: Failed to build gem native extension.
/Users/user/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb --with-mysql-dir=/var/mysql --with-mysql-include=/var/mysql/include --with-mysql-lib=/var/mysql --with-mysql-config=/Applications/XAMPP/xamppfiles/bin/mysql_config
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. please check your installation of mysql and try again.
-----
*** extconf.rb …Run Code Online (Sandbox Code Playgroud) 我的环境
ruby 2.0.0-p195
rails (4.0.0.rc1)
activerecord (4.0.0.rc1)
Run Code Online (Sandbox Code Playgroud)
我想通过id数组对ActiveRecord对象进行排序.我尝试了按字段顺序.
ids = [1,4,2,3]
Foo.where(id: ids).order('FIELD(id, ?)', ids)
Run Code Online (Sandbox Code Playgroud)
然而它失败了.
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?}), ö, ÷, ï, ñ' at line 1:
Run Code Online (Sandbox Code Playgroud)
然后我试试
ids = [1,4,2,3]
Foo.where(id: ids).order('FIELD(id, #{ids.join(","))')
Run Code Online (Sandbox Code Playgroud)
它当然是成功的.但是我担心它可能有SQL注入风险,因为数组ID是从会话值生成的.
有没有更好更安全的方法?
提前致谢.