首先对v进行升级:
v up
安装完毕后运行例子:
import mysql pub fn main() { conn := mysql.connect('localhost', 'root', '123456', 'mysql') res := conn.query('show tables') for row in res.rows() { println(row.vals.join(', ')) } res.free() conn.close() }
运行:
v run test.v
然后就报错了:
fatal error: 'mysql.h' file not found
但是使用的mac中已经安装了mysql,并且能找到mysql.h,已过去之后仍旧有问题,遂寻仙问道,终于找到一个道友刚好解决了这个问题:
$ cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include $ cp -r /usr/local/Cellar/mysql@5.7/5.7.24/include/mysql/* ./
将mysql.h同级的文件全被cp 到指定目录下,福不双至祸不单行,又报了一个错:
ld: library not found for -lmysqlclient clang: error: linker command failed with exit code 1 (use -v to see invocation)V panic: C error. This should never happen. Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
一看是比较常见的mysqlclient的问题,很快就找到了答案:
brew install mysql-connector-c
再次执行例子,得到结果:
hadoop000:test wn$ v run test.v columns_priv db engine_cost event func general_log gtid_executed help_category help_keyword help_relation help_topic innodb_index_stats innodb_table_stats ndb_binlog_index plugin proc procs_priv proxies_priv server_cost servers slave_master_info slave_relay_log_info slave_worker_info slow_log tables_priv time_zone time_zone_leap_second time_zone_name time_zone_transition time_zone_transition_type user