#!/bin/bash
MYSQL="/usr/bin/mysql -uroot "
function create_db()
{
local db_name=${1}
$MYSQL<<!
create database IF NOT EXISTS ${db_name};
!
}
###-------------------------tb_bind_userid_xxx-------------------------------------
function create_table_userid
{
$MYSQL << !
create table if NOT EXISTS bind_pay.tb_bind_userid_${1}(
b_userid bigint not null,
b_membercode bigint not null ,
PRIMARY KEY (b_userid)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
!
}$MYSQL在函数调用之间是持久的,还是每次都要重新连接?
如果它每次都重新连接,我认为create_table_userid不会像预期的那样工作,因为它还没有指定数据库名称。
发布于 2011-04-28 16:10:21
因为每次要创建表时都要调用该函数,所以每次都要调用mysql来连接到数据库。如果你想要持久连接,一种方法是使用现在大多数主流编程语言都支持的mysql库,Perl/Python/Ruby/PHP等。你可以建立一个数据库连接,然后做任何你想做的事情,最后关闭连接。例如在Python/Mysql的文档中
import MySQLdb
conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()正如您所看到的,将打开一个连接conn以连接到数据库。然后使用连接句柄(或者更确切地说是数据库句柄),填充完成,最后,连接关闭。
发布于 2011-05-01 08:40:04
mysql只是一个变量,所以您的代码每次调用这些函数时都会运行$MYSQL。
您可以很容易地创建到mysql的持久连接;只需让您的程序编写其sql来输出,然后将整个结果通过管道传输到mysql:
(
create_table "foo"
create_table "bar"
) | mysql
create_table() {
cat <<!
create table $1 ....
!
}https://stackoverflow.com/questions/5815558
复制相似问题