欢迎来到个人简历网!永久域名:gerenjianli.cn (个人简历全拼+cn)
当前位置:首页 > 范文大全 > 实用文>如何关闭MySQL数据库中错误提示音

如何关闭MySQL数据库中错误提示音

2022-09-11 08:28:37 收藏本文 下载本文

“SuperKyrin”通过精心收集,向本站投稿了10篇如何关闭MySQL数据库中错误提示音,以下是小编为大家整理后的如何关闭MySQL数据库中错误提示音,欢迎参阅,希望可以帮助到有需要的朋友。

如何关闭MySQL数据库中错误提示音

篇1:如何关闭MySQL数据库中错误提示音

使用MySQL的命令终端时,如果输入SQL有误,将有beep声,

如何关闭MySQL数据库中错误提示音

若要关闭该功能,根据mysql --help,使用mysql --no-beep即可。

修改my.ini

在[mysql] 下加入一行

no-beep

最后重新启动MySQL数据库就可以了。

篇2:如何关闭MySQL数据库中错误提示音

使用MySQL的命令终端时,如果输入SQL有误,将有beep声,若要关闭该功能,根据mysql --help,使用mysql --no-beep即可,

修改my.ini

在[mysql] 下加入一行

no-beep

最后重新启动MySQL数据库就可以了。

篇3:如何解决mysql数据库8小时无连接自动关闭

windows下打开my.ini,增加:

interactive_timeout=28800000

wait_timeout=28800000

专家解答:MySQL是一个小型关系型数据库管理系统,由于MySQL体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库,

关于mysql自动关闭服务的现象,可以通过mysql服务器端程序mysql Administrator调整连接参数。将max_connections max_updates max_questions三项数据调整到很大的数字,那么你有限的操作将不会导致数据库服务的终止了在MySQL数据库中,如果一个连接8小时没有请求和操作,就会自动断开,从而导致一些基于数据库连接的应用程序,特别是 WEB 应用程序出错。解决mysql数据库自动关闭服务三个方法:

方法一:这个参数的名称是 wait_timeout,其默认值为 28800秒(8小时)。其意义为关闭一个连接之前在这个连接上等到行动的秒数,也就是说,如果一个连接闲置超过这个选项所设置的秒数,MySQL 会主动断开这个连接。

修改操作:

linux下打开/etc/my.cnf,在属性组mysqld下面添加参数如下:

interactive_timeout=28800000

wait_timeout=28800000

windows下打开my.ini,增加:

interactive_timeout=28800000

wait_timeout=28800000

有实践表明,没有办法把这个值设置成无限大,即永久,

因此如果你无法保证你的应用程序必定在设定的秒数内至少有一次操作,那么最好用第二个方法解决这个问题。

方法二:修改如下JDBC连接的 URL:

jdbc:mysql://hostaddress:3306/schemaname?autoReconnect=true

添加 autoReconnect=true 这个参数,即能解决这个问题。

方法三:配置文件(proxool.xml):

mysql

jdbc:mysql://localhost/yourDatebase?useUnicode=true&characterEncoding=UTF-8

com.mysql.jdbc.Driver

90000

20

3

20

3

true

true

SELECT CURRENT_USER

专家建议关于mysql自动关闭服务的三个方法,用户最好采取第一个办法最为彻底解决。

篇4:MySQL的常见错误的解决方法(英文)数据库

Common Errors When Using MySQL Programs This section lists some errors that users frequently encounter when running MySQL programs. Although the problems show up when you try to run client programs, the solutions to many of the problems in

Common Errors When Using MySQL Programs

This section lists some errors that users frequently encounter when running MySQL programs. Although the problems show up when you try to run client programs, the solutions to many of the problems involves changing the configuration of the MySQL server.

1. Aclearcase/“ target=”_blank“ >ccess denied

An Access denied error can have many causes. Often the problem is related to the MySQL accounts that the server allows client programs to use when connecting. See Section 5.7.8, “Causes of Access denied Errors”. See Section 5.7.2, “How the Privilege System Works”.

2. Can't connect to [local] MySQL server

A MySQL client on Unix can connect to the mysqld server in two different ways: By using a Unix socket file to connect through a file in the filesystem (default /tmp/mysql.sock), or by using TCP/IP, which connects through a port number. A Unix socket file connection is faster than TCP/IP, but can be used only when connecting to a server on the same computer. A Unix socket file is used if you don't specify a hostname or if you specify the special hostname localhost.

If the MySQL server is running on Windows 9x or Me, you can connect only via TCP/IP. If the server is running on Windows NT, , XP, or and is started with the --enable-named-pipe option, you can also connect with named pipes if you run the client on the host where the server is running. The name of the named pipe is MySQL by default. If you don't give a hostname when connecting to mysqld, a MySQL client first tries to connect to the named pipe. If that doesn't work, it connects to the TCP/IP port. You can force the use of named pipes on Windows by using . as the hostname.

The error Can't connect to ... normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket filename or TCP/IP port number when trying to connect to the server.

Start by checking whether there is a process named mysqld running on your server host. (Use ps xa | grep mysqld on Unix or the Task Manager on Windows.) If there is no such process, you should start the server. See Section 2.9.2.3, “Starting and Troubleshooting the MySQL Server”.

If a mysqld process is running, you can check it by trying the following commands. The port number or Unix socket filename might be different in your setup. host_ip represents the IP number of the machine where the server is running.

shell> mysqladmin version

shell> mysqladmin variables

shell> mysqladmin -h `hostname` version variables

shell> mysqladmin -h `hostname` --port=3306 version

shell> mysqladmin -h host_ip version

shell> mysqladmin --protocol=socket --socket=/tmp/mysql.sock version

Note the use of backticks rather than forward quotes with the hostname command; these cause the output of hostname (that is, the current hostname) to be substituted into the mysqladmin command. If you have no hostname command or are running on Windows, you can manually type the hostname of your machine (without backticks) following the -h option. You can also try -h 127.0.0.1 to connect with TCP/IP to the local host.

Here are some reasons the Can't connect to local MySQL server error might occur:

mysqld is not running. Check your operating system's process list to ensure the mysqld process is present.

You're running a MySQL server on Windows with many TCP/IP connections to it. If you're experiencing that quite often your clients get that error, you can find a workaround here: Section 2.1, “Connection to MySQL Server Failing on Windows”.

You are running on a system that usesMIT-pthreads. If you are running on a system that doesn't have native threads, mysqld uses the MIT-pthreads package. See Section 2.1.1, “Operating Systems Supported by MySQL”. However, not all MIT-pthreads versions support Unix socket files. On a system without socket file support, you must always specify the hostname explicitly when connecting to the server. Try using this command to check the connection to the server:

shell> mysqladmin -h `hostname` version

Someone has removed the Unix socket file that mysqld uses (/tmp/mysql.sock by default). For example, you might have a cron job that removes old files from the /tmp directory. You can always run mysqladmin version to check whether the Unix socket file that mysqladmin is trying to use really exists. The fix in this case is to change the cron job to not remove mysql.sock or to place the socket file somewhere else. See Section A.4.5, “How to Protect or Change the MySQL Socket File /tmp/mysql.sock”.

You have started the mysqld server with the --socket=/path/to/socket option, but forgotten to tell client programs the new name of the socket file. If you change the socket pathname for the server, you must also notify the MySQL clients. You can do this by providing the same --socket option when you run client programs. You also need to ensure that clients have permission to access the mysql.sock file. To find out where the socket file is, you can do:

shell> netstat -ln | grep mysql

See Section A.4.5, “How to Protect or Change the MySQL Socket File /tmp/mysql.sock”.

You are using Linux and one server thread has died (dumped core). In this case, you must kill the other mysqld threads (for example, with kill or with the mysql_zap script) before you can restart the MySQL server. See Section A.4.2, “What to Do If MySQL Keeps Crashing”.

The server or client program might not have the proper access privileges for the directory that holds the Unix socket file or the socket file itself. In this case, you must either change the access privileges for the directory or socket file so that the server and clients can access them, or restart mysqld with a --socket option that specifies a socket filename in a directory where the server can create it and where client programs can access it.

If you get the error message Can't connect to MySQL server on some_host, you can try the following things to find out what the problem is:

Check whether the server is running on that host by executing telnet some_host 3306 and pressing the Enter key a couple of times. (3306 is the default MySQL port number. Change the value if your server is listening to a different port.) If there is a MySQL server running and listening to the port, you should get a response that includes the server's version number. If you get an error such as telnet: Unable to connect to remote host: Connection refused, then there is no server running on the given port.

If the server is running on the local host, try using mysqladmin -h localhost variables to connect using the Unix socket file. Verify the TCP/IP port number that the server is configured to listen to (it is the value of the port variable.)

Make sure that your mysqld server was not started with the --skip-networking option. If it was, you cannot connect to it using TCP/IP.

Check to make sure that there is no firewall blocking access to MySQL. Applications such as ZoneAlarm and the Windows XP personal firewall may need to be configured to allow external access to a MySQL server.

2.1. Connection to MySQL Server Failing on Windows

When you're running a MySQL server on Windows with many TCP/IP connections to it, and you're experiencing that quite often your clients get a Can't connect to MySQL server error, the reason might be that Windows doesn't allow for enough ephemeral (short-lived) ports to serve those connections.

By default, Windows allows 5000 ephemeral (short-lived) TCP ports to the user. After any port is closed it will remain in a TIME_WAIT status for 120 seconds. This status allows the connection to be reused at a much lower cost than re-initialising a brand new connection. However, the port will not be available again until this time expires.

With a small stack of available TCP ports (5000) and a high number of TCP ports being open and closed over a short period of time along with the TIME_WAIT status you have a good chance for running out of ports. There are two ways to address this problem:

Reduce the number of TCP ports consumed quickly by investigating connection pooling or persistent connections where possible

Tune some settings in the Windows registry (see below)

IMPORTANT: The following procedure involves modifying the Windows registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, view the following article in the Microsoft Knowledge Base: support.microsoft.com/kb/256986/EN-US/.

Start Registry Editor (Regedt32.exe).

Locate the following key in the registry:

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParameters

On the Edit menu, click Add Value, and then add the following registry value:

Value Name: MaxUserPort

Data Type: REG_DWORD

Value: 65534

This sets the number of ephemeral ports available to any user. The valid range is between 5000 and 65534 (decimal). The default value is 0x1388 (5000 decimal).

On the Edit menu, click Add Value, and then add the following registry value:

Value Name: TcpTimedWaitDelay

Data Type: REG_DWORD

Value: 30

This sets the number of seconds to hold a TCP port connection in TIME_WAIT state before closing. The valid range is between 0 (zero) and 300 (decimal). The default value is 0x78 (120 decimal).

Quit Registry Editor.

Reboot the machine.

Note: Undoing the above should be as simple as deleting the registry entries you've created.

3. Client does not support authentication protocol

MySQL 5.0 uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older (pre-4.1) clients. If you upgrade the server from 4.1, attempts to connect to it with an older client may fail with the following message:

shell> mysql

Client does not support authentication protocol requested

by server; consider upgrading MySQL client

To solve this problem, you should use one of the following approaches:

Upgrade all client programs to use a 4.1.1 or newer client library.

When connecting to the server with a pre-4.1 client program, use an account that still has a pre-4.1-style. password.

Reset the password to pre-4.1 style. for each user that needs to use a pre-4.1 client program. This can be done using the SET PASSWORD statement and the OLD_PASSWORD() function:

mysql> SET PASSWORD FOR

-> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');

Alternatively, use UPDATE and FLUSH PRIVILEGES:

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')

-> WHERE Host = 'some_host' AND User = 'some_user';

mysql> FLUSH PRIVILEGES;

Substitute the password you want to use for “newpwd” in the preceding examples. MySQL cannot tell you what the original password was, so you'll need to pick a new one.

Tell the server to use the older password hashing algorithm:

Start mysqld with the --old-passwords option.

Assign an old-format password to each account that has had its password updated to the longer 4.1 format. You can identify these accounts with the following query:

mysql> SELECT Host, User, Password FROM mysql.user

-> WHERE LENGTH(Password) > 16;

For each account record displayed by the query, use the Host and User values and assign a password using the OLD_PASSWORD() function and either SET PASSWORD or UPDATE, as described earlier.

共3页: 1 [2] [3] 下一页

原文转自:www.ltesting.net

篇5:Mysql和Oracle数据库中的分页查询

方法一:mysql数据库分页

<%

//定义每一页显示的记录

int pageSize = 3;

String strPageNo = request.getParameter(”pageNo“);

//记录当前页面

int pageNo;

//如果传入的当前页码为空,停留在第一页

if(strPageNo == null || strPageNo.equals(”“))

{

pageNo = 1;

}

else

{

try

//把传进来的字符串转化成数字

{

pageNo = Integer.parseInt(strPageNo.trim());

}

catch(NumberFormatException e)

{

pageNo = 1;

}

//如果页码小于或者等于0停留在第一页

if(pageNo<=0)

{

pageNo=1;

}

}

//连接数据库

Class.forName(”com.mysql.jdbc.Driver“);

Connection conn = DriverManager.getConnection

(”jdbc:mysql://localhost/bbs?user=root&password=mingming“);

Statement stCount = conn.createStatement();

ResultSet rsCount = stCount.executeQuery

(”select count(*) from article where pid=0“);//查询共有多少个根节点

rsCount.next();

int totalRecord = rsCount.getInt(1); //拿到所有的子节点,计算出总共有多少条记录

// 第一种方法    计算得到总的页数,如果能被整数,页数就是商,否则就是商+1

int totalPage =

totalRecord%pageSize==0 ? totalRecord/pageSize : totalRecord/pageSize +1;

//第二种方法  计算得到总的页数,在 总记录上+加一个pageSize然后减去1除页面的大小pageSize,取商

int totalRecords = rsCount.getInt(1);

totalPages = (totalRecords + PAGE_SIZE - 1)/PAGE_SIZE;

//如果当前页码大于总的页数,停在最后一页

if(pageNo>totalPage)

{

pageNo = totalPage;

}

//计算每一次分页时 的起始位置,注意起始是从0开始;

int startPos = (pageNo-1)*pageSize;

Statement st = conn.createStatement();

//进行分页查询,startPos是每一次分页的起始位置; pageSize是这一页要显示记录的大小

ResultSet rs = st.executeQuery

(”select * from article where pid =0 order by pdate desc limit “+startPos+”,“+pageSize);

%>

分页后在页面的不同的显示方式:

方式一:在一个table中正常的显示:

<%

while(rs.next()){

%>

<%= rs.getString(”title“) %>//只显示每一个记录的title

<%

}

//关闭流

rs.close();

st.close();

conn.close();

%>

首页<%=1 %> 共<%=totalPage %>页  第<%=pageNo %>页 末页<%=totalPage %>

”> 上一页

“> 下一页

方式二:可以选择的显示:可以实现页面的跳转

<%

for(int i = 1;i<=totalPage;i++){

%>

<%=i %> <%= pageNo==i?”selected“:”“%>>第<%=i %>页

<%

}

%>

方式三:可以直接进行页面的查找:

//提交到当前页面

<%=pageNo%> />

方法二:Oracle数据库下的分页操作

分页查询

姓名

薪水

<%

//连接数据库,加载驱动

Class.forName(”oracle.jdbc.driver.OracleDriver“);

Connection ct = DriverManager.getConnection

(”jdbc:oracle:thin:@127.0.0.1:1521:ORCL“,”scott“,”tiger“);

Statement sm = ct.createStatement();

//总共有多少页

int pageCount = 0;

//总共有多少行记录

int rowCount = 0;

//每页有3行记录

int pageSize = 3;

//接收pageNow

String s_pageNow = (String)request.getParameter(”pageNow“);

//当前在第一页

int pageNow = 1;

if(s_pageNow != null)

{

//把s_pageNow转化为数值型

pageNow = Integer.parseInt(s_pageNow);

}

//查询表中共有多少条记录www.dnzg.cn

ResultSet rs = sm.executeQuery(”select count(*) from emp“);

if(rs.next())

{

rowCount = rs.getInt(1);

//如果整除就是商,否则就是商加上1 可以用三元表达式代替 rowCount%pageSize==0 ? rowCount/pageSize : rowCount/pageSize +1

if(rowCount%pageSize == 0)

{

pageCount = rowCount/pageSize;

}

else

{

pageCount = rowCount/pageSize +1;

}

}

//执行分页查询

rs = sm.executeQuery

(”select * from (select a1.*,rownum rn from(select * from emp) a1 where rownum<=“+pageNow*pageSize+”)where rn>=“+((pageNow-1)*pageSize+1)+” “);

while(rs.next())

{

out.println(”“);

out.println(”“+rs.getString(2)+”“);

out.println(”“+rs.getString(6)+”“);

out.println(”“);

}

//输出每一页要查找的连接

for(int i=1; i<=pageCount; i++)

{

out.print(”[“+i+”]“);

}

%>

篇6:python中使用mysql数据库详细介绍

这篇文章主要介绍了python中使用mysql数据库详细介绍,本文起讲解了安装mysql、安装MySQL-python、mysql 的基本操作、python 操作mysql数据库基础等内容,需要的朋友可以参考下

一、安装mysql

如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可,

Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装:

Ubuntudeepin

代码如下:

>>sudo apt-get install mysql-server

>>Sudo apt-get install mysql-client

centOS/redhat

代码如下:

>>yum install mysql

二、安装MySQL-python

要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块。

下载地址:pypi.python.org/pypi/MySQL-python/

下载MySQL-python-1.2.5.zip 文件之后直接解压。进入MySQL-python-1.2.5目录:

代码如下:

>>python setup.py install

三、测试

测试非常简单,检查MySQLdb 模块是否可以正常导入。

代码如下:

fnngj@fnngj-H24X:~/pyse$ python

Python 2.7.4 (default, Sep 26 , 03:20:56)

[GCC 4.7.3] on linux2

Type ”help“, ”copyright“, ”credits“ or ”license“ for more information.

>>> import MySQLdb

没有报错提示MySQLdb模块找不到,说明安装OK ,下面开始使用python 操作数据库之前,我们有必要来回顾一下mysql的基本操作:

四、mysql 的基本操作

代码如下:

$ mysql -u root -p (有密码时)

$ mysql -u root    (无密码时)

代码如下:

mysql> show databases; // 查看当前所有的数据库

+--------------------+

| Database          |

+--------------------+

| information_schema |

| csvt              |

| csvt04            |

| mysql             |

| performance_schema |

| test              |

+--------------------+

6 rows in set (0.18 sec)

mysql> use test;  //作用与test数据库

Database changed

mysql> show tables;  //查看test库下面的表

Empty set (0.00 sec)

//创建user表,name 和password 两个字段

mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec)

//向user表内插入若干条数据

mysql> insert into user values(‘Tom‘,‘1321‘);

Query OK, 1 row affected (0.05 sec)

mysql> insert into user values(‘Alen‘,‘7875‘);

Query OK, 1 row affected (0.08 sec)

mysql> insert into user values(‘Jack‘,‘7455‘);

Query OK, 1 row affected (0.04 sec)

//查看user表的数据

mysql> select * from user;

+------+----------+

| name | password |

+------+----------+

| Tom | 1321    |

| Alen | 7875    |

| Jack | 7455    |

+------+----------+

3 rows in set (0.01 sec)

//删除name 等于Jack的数据

mysql> delete from user where name = ‘Jack‘;

Query OK, 1 rows affected (0.06 sec)

//修改name等于Alen 的password 为 1111

mysql> update user set password=‘1111‘ where name = ‘Alen‘;

Query OK, 1 row affected (0.05 sec)

Rows matched: 1 Changed: 1 Warnings: 0

//查看表内容

mysql> select * from user;

+--------+----------+

| name  | password |

+--------+----------+

| Tom   | 1321    |

| Alen  | 1111    |

+--------+----------+

3 rows in set (0.00 sec)

五、python 操作mysql数据库基础

代码如下:

#coding=utf-8

import MySQLdb

conn= MySQLdb.connect(

host=‘localhost‘,

port = 3306,

user=‘root‘,

passwd=‘123456‘,

db =‘test‘,

)

cur = conn.cursor

#创建数据表

#cur.execute(”create table student(id int ,name varchar(20),class varchar(30),age varchar(10))“)

#插入一条数据

#cur.execute(”insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)“)

#修改查询条件的数据

#cur.execute(”update student set class=‘3 year 1 class‘ where name = ‘Tom‘“)

#删除查询条件的数据

#cur.execute(”delete from student where age=‘9‘“)

cur.close()

conn.commit()

conn.close()

代码如下:

>>> conn = MySQLdb.connect(host=‘localhost‘,port = 3306,user=‘root‘, passwd=‘123456‘,db =‘test‘,)

Connect() 方法用于创建数据库的连接,里面可以指定参数:用户名,密码,主机等信息,

这只是连接到了数据库,要想操作数据库需要创建游标。

代码如下:

>>> cur = conn.cursor()

通过获取到的数据库连接conn下的cursor()方法来创建游标。

代码如下:

>>> cur.execute(”create table student(id int ,name varchar(20),class varchar(30),age varchar(10))“)

通过游标cur 操作execute()方法可以写入纯sql语句。通过execute()方法中写如sql语句来对数据进行操作。

代码如下:

>>>cur.close()

cur.close() 关闭游标

代码如下:

>>>conn.commit()

conn.commit()方法在提交事物,在向数据库插入一条数据时必须要有这个方法,否则数据不会被真正的插入。

代码如下:

>>>conn.close()

Conn.close()关闭数据库连接

六、插入数据

通过上面execute()方法中写入纯的sql语句来插入数据并不方便。如:

代码如下:

>>>cur.execute(”insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)“)

我要想插入新的数据,必须要对这条语句中的值做修改。我们可以做如下修改:

代码如下:

#coding=utf-8

import MySQLdb

conn= MySQLdb.connect(

host=‘localhost‘,

port = 3306,

user=‘root‘,

passwd=‘123456‘,

db =‘test‘,

)

cur = conn.cursor()

#插入一条数据

sqli=”insert into student values(%s,%s,%s,%s)“

cur.execute(sqli,(‘3‘,‘Huhu‘,‘2 year 1 class‘,‘7‘))

cur.close()

conn.commit()

conn.close()

假如要一次向数据表中插入多条值呢?

代码如下:

#coding=utf-8

import MySQLdb

conn= MySQLdb.connect(

host=‘localhost‘,

port = 3306,

user=‘root‘,

passwd=‘123456‘,

db =‘test‘,

)

cur = conn.cursor()

#一次插入多条记录

sqli=”insert into student values(%s,%s,%s,%s)“

cur.executemany(sqli,[

(‘3‘,‘Tom‘,‘1 year 1 class‘,‘6‘),

(‘3‘,‘Jack‘,‘2 year 1 class‘,‘7‘),

(‘3‘,‘Yaheng‘,‘2 year 2 class‘,‘7‘),

])

cur.close()

conn.commit()

conn.close()

executemany()方法可以一次插入多条值,执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数。

七、查询数据

也许你已经尝试了在python中通过

代码如下:

>>>cur.execute(”select * from student“)

来查询数据表中的数据,但它并没有把表中的数据打印出来,有些失望。

来看看这条语句获得的是什么

代码如下:

>>>aa=cur.execute(”select * from student“)

>>>print aa

5

它获得的只是我们的表中有多少条数据。那怎样才能获得表中的数据呢?进入python shell

代码如下:

>>> import MySQLdb

>>> conn = MySQLdb.connect(host=‘localhost‘,port = 3306,user=‘root‘,   passwd=‘123456‘,db =‘test‘,)

>>> cur = conn.cursor()

>>> cur.execute(”select * from student“)

5L

>>> cur.fetchone()

(1L, ‘Alen‘, ‘1 year 2 class‘, ‘6‘)

>>> cur.fetchone()

(3L, ‘Huhu‘, ‘2 year 1 class‘, ‘7‘)

>>> cur.fetchone()

(3L, ‘Tom‘, ‘1 year 1 class‘, ‘6‘)

...

>>>cur.scroll(0,‘absolute‘)

fetchone()方法可以帮助我们获得表中的数据,可是每次执行cur.fetchone() 获得的数据都不一样,换句话说我没执行一次,游标会从表中的第一条数据移动到下一条数据的位置,所以,我再次执行的时候得到的是第二条数据。

scroll(0,‘absolute‘) 方法可以将游标定位到表中的第一条数据。

还是没解决我们想要的结果,如何获得表中的多条数据并打印出来呢?

代码如下:

#coding=utf-8

import MySQLdb

conn= MySQLdb.connect(

host=‘localhost‘,

port = 3306,

user=‘root‘,

passwd=‘123456‘,

db =‘test‘,

)

cur = conn.cursor()

#获得表中有多少条数据

aa=cur.execute(”select * from student“)

print aa

#打印表中的多少数据

info = cur.fetchmany(aa)

for ii in info:

print ii

cur.close()

conn.commit()

conn.close()

通过之前的print aa 我们知道当前的表中有5条数据,fetchmany()方法可以获得多条数据,但需要指定数据的条数,通过一个for循环就可以把多条数据打印出啦!执行结果如下:

代码如下:

5

(1L, ‘Alen‘, ‘1 year 2 class‘, ‘6‘)

(3L, ‘Huhu‘, ‘2 year 1 class‘, ‘7‘)

(3L, ‘Tom‘, ‘1 year 1 class‘, ‘6‘)

(3L, ‘Jack‘, ‘2 year 1 class‘, ‘7‘)

(3L, ‘Yaheng‘, ‘2 year 2 class‘, ‘7‘)

[Finished in 0.1s]

篇7:需要避免的MYSQL客户机程序设计错误数据库教程

6.8 其他主题

本节包括几个主题,这些主题不完全适合于本章从client1到client5 的开发中的任一小节的内容:

■ 在使用结果集元数据帮助验证这些数据适合于计算之后,使用结果集数据计算结果,

■ 如何处理很难插入到查询中的数据。

■ 如何处理图形数据。

■ 如何获得表结构的信息。

■ 常见的MySQL程序设计错误及如何避免。

6.8.1在结果集上执行计算

迄今为止,我们集中而主要地使用了结果集元数据来打印行数据,但很明显,除打印之外,还有需要使用数据做其他事情的时候。例如,计算基于数据值的统计信息,应用元数据确保数据适合它们要满足的需求。哪种类型的需求?对于启动程序来说,可能要校验一下正

要执行数字计算的列实际上是否包含着数字!

下面的列表显示了一个简单函数summary _ stats ( ) ,它获取结果集和列索引,并产生列值的汇总统计。该函数还列出缺少数值的数量,它是通过检查NULL 来检测的。这些计算包括两个数据所必须满足的需求, summary_stats() 用结果集元数据来校验:

■ 指定的列必须存在(也就是说,列索引必须在结果集列值的范围内)。

■ 此列必须包括数字值。

如果这些条件不满足,则summary_stats() 只打印出错误消息并返回。代码如下:

请注意在mysql_fetch_row() 循环前面调用的mysql_ data _ seek( )。为获得同样的结果集,它允许多次调用summary _ stats()(假设要计算几列的统计值的话)。每次调用summary _ stats( )都要“重新回到”到结果集的开始(这里假设mysql_store_result() 创建结果集,如果用mysql_use_result() 创建结果集就只能按顺序处理行,而且只能处理一次)。summary_stats() 是个相对简单的函数,但它给我们一个提示,就是如何编写一个比较复杂的计算程序,如两个列的最小二乘回归或者标准统计,如t -检验。

6.8.2 对查询中有疑问的数据进行编码

包括引号、空值和反斜线的数据值,如果把它们插入到查询中,在执行查询时就会产生一些问题。下面的讨论论述了这些难点,并介绍了解决的办法。假设要建造一个SELECT 查询,它基于由name 指向的空终结串的内容:

如果name 的值类似于“0’Malley, Brian”,这时进行的查询就是非法的,因为引号在引用的字符串里出现:

需要特别注意这个引号,以便使服务器不将它解释为name 的结尾。一种方法是在字符串内使用双引号,这就是ANSI SQL 约定。SQL 支持这个约定,也允许引号在反斜线后使用:

另一个有问题之处是查询中任意二进制数据的使用,例如,在把图形存储到数据库这样的应用程序中会发生这种情况。因为二进制数值含有一些字符,把它放到查询中是不安全的。为了解决这个问题,可使用mysql_ escape _ string( ),它可以对特殊字符进行编码,使其在引用的字符串中可以使用。mysql_escape_string() 认为的特殊字符是指空字符、单引号、双引号、反斜线、换行符、回车符和C ontrol - Z(最后一个在Windows 语言环境中出现)。什么时候使用mysql_escape_string() 呢?最保险的回答是“始终”。然而,如果确信数据的形式并且知道它是正确的―可能因为预先执行了确认检查―就不必编码了。例如,如果处理电话号码的字符串,它完全由数字和短线组成,那么就不必调用mysql_ escape _ string( )了,否则还是要调用。

mysql_escape_string() 对有问题的字符进行编码是将它们转换为以反斜线开头的2个字符的序列。例如,空字符转换为‘ 0’,这里的0 是可打印的ASCII 码0,而不是空。反斜线、单引号和双引号分别转换为‘ ’、‘’’和‘”’。调用mysql_escape_string() 的过程如下:

mysql_escape_string() 对from_str 进行编码,并把结果写入to _ str中,还添加了空终结值,这样很方便,因为可以利用像strcpy() 和strlen() 这样的函数使用该结果串。from_str 指向包括将要编码的字符串的char 缓冲区,这个字符串可能包含任何内容,其中包括二进制数据。to_str 指向一个存在的char 缓冲区,在这个缓冲区里,可以写入编码的字符串;不要传递未初始化的指针或NULL 指针,希望由mysql_escape_string() 分配空间。由to_str 指向的缓冲区的长度至少是(from_len*2)+1个字节(很可能from_str 中的每个字符都需要用2 个字符来编码;额外的字节是空终结值)。

from_len 和to_len 都是unsigned int 值,from_len 表示from_str 中数据的长度;提供这个长度是非常必要的,因为from_str 可能包含空值字节,不能把它当作空终结串。从mysql_escape_string() 返回的to_len 值是作为结果的编码字符串的实际长度,没有对空终结值进行计数。

当mysql_escape_string() 返回时, to _ str中编码的结果就可看作是空终结串,因为from_str 中的空值都被编码为‘ 0’。

为了重新编写构造SELECT 的代码,使名称的值即使包含引号也能工作,我们进行下面的操作:

6.8.3 图像数据的处理

mysql_escape_string() 的基本功能之一就是把图像数据加载到一个表中,

本节介绍如何进行这项工作(这个讨论也适用于二进制数据的其他形式)。假设想从文件中读取图像,并将它们连同唯一的标识符存储到表中。BLOB 类型对二进制数据来讲是个很好的选择,因此可以使用下面的表说明:

实际上,要想从文件中获取图像并放入images 表,利用下面的函数load_image() 可以实现,给出一个标识符号码和一个指向包括这个图像数据的打开文件的指针:

load_image() 不会分配非常大的查询缓冲区( 10 0 K),因此它只能处理相对较小的图形。

在实际的应用程序中,可以根据图形文件的大小动态地分配缓冲区。处理从数据库中恢复的图形数据(或任何二进制数据)并不像开始把它放入时那样问题重重,因为在变量MYSQL_ROW 中数据值的原始形式是有效的,通过调用mysql_ fetch _length ( ),这个长度也是有效的。必须将值看作是计数串,而不是空终结串。

6.8.4 获取表信息

MySQL允许使用下面的查询获取有关表结构的信息(下面两者是等价的):

与SELECT 相类似,两个语句都返回结果集。为了在表中找出有关列,所需做的就是处理结果集中的行,从中获取有用的信息。例如,如果从mysql客户机上发布DESCRIBE images 语句,就会返回这样的信息:

如果从自己的客户机上执行同样的查询,可以得到相同的信息(没有边框)。如果只想要单个列的信息,则使用如下这个查询:

SHOW FIELDS FROM tbl _ name LIKE “col _ name”

此查询会返回相同的列,但只是一行(如果列不存在就不返回行)。

6.8.5 需要避免的客户机程序设计错误

本节讨论一些常见的MySQLC API 程序设计错误,以及如何避免其发生(这些问题在MySQL邮件清单中会周期性地突然出现)。

1. 错误1――使用未初始化的连接处理程序指针在本章的样例中,我们已经通过传递NULL 参数调用了mysql_ i n i t ( ),这就是让它分配并且初始化MYSQL 结构,然后返回一个指针。另外一种方法是将指针传递到一个已有的MYSQL 结构中。在这种情况下, mysql_init() 会将结构初始化并返回一个指针,而不必自己分配结构。如果要使用第二种方法,则要小心会出现一些微妙的问题。下面的讨论指出了需要注意的一些问题。如果将一个指针传递给mysql_ init( ),它应该实际指向某些东西。看下面的代码段:

这个问题是,mysql_init() 得到了一个指针,但指针没有指向所知的任何地方。conn 是一个局部变量,因此在main() 开始执行时它是一个能指向任何地方的未初始化的存储器,这就是说mysql_init() 将使用指针,并可在内存的一些任意区域滥写。如果幸运的话, conn 将指向您的程序地址空间的外部,这样,系统将立即终止,使您能尽早意识到代码中出现的问题。

如果不幸的话, conn 将指向程序中以后才使用的一些数据的内部,直到再次使用那个数据时才发现问题。因此实际出现问题的地方远比执行程序时出现的问题多,也更难捕捉到。下面是一段有问题的代码:

此时, conn 是一个全局变量,因此在程序启动前,将它初始化为0(就是NULL)。mysql_init() 遇到NULL 参数,因此初始化并分配一个新的连接处理程序。只要将conn 传递给需要非NULL 连接处理程序的MySQLCAPI 函数,系统就会崩溃。这些代码段的修改就是确保conn 有一个可知的值。例如,可以将它初始化到已经分配的MYSQL 结构地址中去:

然而,推荐的(较容易的!)解决方案仅仅是将NULL 显式地传递给mysql_ init( ),让该函数分配MYSQL 结构,并将返回值赋值给conn:

无论如何不要忘记检验mysql_init() 的返回值,以确保它不是NULL。

2. 错误2――有效结果集检验的失败

请记住检查希望得到的结果集的调用状态。下面的代码没有做到这一点:

不幸地是,如果mysql_store_result() 失败,res_set 为NULL,while 循环也不执行了,应测试返回结果集函数的返回值,以确保实际上在进行工作。

3. 错误3―― NULL 列值引起的失败

不要忘记检查mysql_fetch_row() 返回的数组MYSQL_ROW 中列值是否为NULL 指针。如果row[i] 为NULL,则在一些机器上,下面的代码就会引起崩溃:

该错误危害最大的部分是,有些printf() 的版本很宽容地对NULL指针输出了“( null )”,这就使错误很容易逃脱而没有把错误定位。如果把程序给了朋友,而他只有不太宽容printf( )版本,程序就会崩溃,您的朋友会认为您是个无用的程序员。循环应该写成下面这样:

不需要检查列值是否为NULL 的惟一一次是当已经从列信息结构确定IS _ NOT _ NULL( )为真时。

4. 错误4――传递无意义的结果缓冲区

需要您提供缓冲区的客户机库函数通常要使这些缓冲区真正存在,下面的代码违反了这个规则:

问题是什么呢?to_str 必须指向一个存在的缓冲区,而在这个样例中没有,因此,它指向了随意的位置。不要向mysql_escape_string 传递无意义的指针作为to_str 参数,否则它会恣意践踏内存。

篇8:linux中mysql数据库备份与恢复linux操作系统

在linux中对数据库或表进行备份与恢复我们会要用于数据库常用命令mysqldump,下面我来利用mysqldump对mysql数据库进行备份与恢复操作,

备份与恢复

比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump

命令格式如下:

[root@linuxsir01 root]# mysqldump -u root -p linux > /root/linux.sql

Enter password:在这里输入数据库的密码

例如:将上例创建的aaa库备份到文件back_aaa中

1、备份

代码如下复制代码

[root@test1 root]# cd /home/data/mysql (进入到库目录,本例库已由val/lib/mysql转到/home/data/mysql,见

上述第七部分内容)

代码如下复制代码

[root@test1 mysql]# mysqldump -u root -p –opt aaa > back_aaa

2、恢复

代码如下复制代码

[root@test mysql]# mysql -u root -p ccc < back_aaa

篇9:MySQL数据库中的外键约束详解

教程贴士:使用MySQL开发过数据库驱动的小型web应用程序的人都知道,对关系数据库的表进行创建、检索、更新和删除等操作都是些比较简单的过程

使用MySQL开发过数据库驱动的小型web应用程序的人都知道,对关系数据库的表进行创建、检索、更新和删除等操作都是些比较简单的过程,理论上,只要掌握了最常见的SQL语句的用法,并熟悉您选择使用的服务器端脚本语言,就足以应付对MySQL表所需的各种操作了,尤其是当您使用了快速MyISAM数据库引擎的时候。但是,即使在最简单的情况下,事情也要比我们想象的要复杂得多。下面我们用一个典型的例子进行说明。假设您正在运行一个博客网站,您几乎天天更新,并且该站点允许访问者评论您的帖子。

在这种情况下,我们的数据库模式至少应该包括两个MyISAM表,一个用于存放您的博客文章,另一个来处理访问者的评论。很明显,这两个表之间存在一个一对多的关系,所以我们要在第二个表中定义一个外键,以便在更新或者删除数据行时可以保持数据库的完整性。

像上面这样的应用程序,不仅维护两个表的完整性是一个严峻的挑战,而最大的难点在于我们必须在应用程序级别来维护它们的完整性。这是大部分不要求使用事务的web项目在开发期间所采取的方法,因为MyISAM表可以提供出色的性能。

当然,这样做也是有代价的,正如我前面所说的,应用程序必须维护数据库的完整性和一致性,这就意味着要实现更复杂的程序设计逻辑来处理各个表之间的关系。虽然可以通过使用抽象层和ORM模块来简化数据库访问,但是随着应用程序所需数据表的数量的增加,处理它们所需的逻辑无疑也会随之变得越发复杂。

那么,对于MySQL来说,有没有数据库级别的外键处理方式来帮助维护数据库完整性的呢? 幸运的是,答案是肯定的!MySQL还可以支持InnoDB表,使我们可以通过一种非常简单的方式来处理外键约束。这个特性允许我们可以触发器某些动作,诸如更新和删掉表中的某些数据行以维护预定义的关系。

凡事有利皆有弊,使用InnoDB表的主要缺点是它们的速度要比MyISAM慢,尤其是在必须查询许多表的大规模应用程序中,这一点尤为明显。好在较新版本MySQL的MyISAM表也已支持外键约束。

本文将介绍如何将外键约束应用于InnoDB表。此外,我们还将使用一个简单的基于PHP的MySQL抽象类来创建有关的示例代码;当然,您也可以使用自己喜欢的其它服务器端语言。现在,我们开始介绍如何将外键约束应用于MySQL。

使用外键约束的时机

老实说,在MySQL中使用InnoDB表的时候,不一定非用外键约束不可,然而,为了外键约束在某些情况下的功用,我们将通过前面提到的例子的代码进行具体说明。它包括两个MyISAM表,分别用于存放博客文章和评论。

定义数据库模式时,我们要在这两个表之间建立起一对多的关系,方法是在存放评论的表中创建一个外键,以将其中的数据行(即评论)对应到特定的博客文章。下面是创建示例MyISAM表的基本SQL代码:

DROP TABLE IF EXISTS `test`.`blogs`;

CREATE TABLE `test`.`blogs` (

`id` INT(10) UNSIGNED AUTO_INCREMENT,

`title` TEXT,

`content` TEXT,

`author` VARCHAR(45) DEFAULT NULL,

PRIROSE KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `test`.`comments`;

CREATE TABLE `test`.`comments` (

`id` INT(10) UNSIGNED AUTO_INCREMENT,

`blog_id` INT(10) UNSIGNED DEFAULT NULL,

`comment` TEXT,

`author` VARCHAR(45) DEFAULT NULL,

PRIROSE KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

上面,我们只是定义了两个MyISAM表,它们构成了博客应用程序的数据层,

如您所见,第一个表名为blogs,它由一些含义很明显的字段组成,分别用于存放每篇博客文章的ID、标题和内容,最后是作者。第二个表名为comments,用于存放各篇博客文章的有关评论,它将博客文章的ID作为它的外键,从而建立起一对多的关系。

迄今为止,我们的工作还算轻松,因为我们只是创建了两个简单的MyISAM表。下一步,我们要做的是使用一些记录来填充这些表,以便进一步演示在第一个表中删除表项时,应该在另一个表中执行那些操作。

更新并维护数据库的完整性

前面部分,我们创建了两个MyISAM表,来充当博客应用程序的数据层。当然,上面的介绍还很简单,我们需要做进一步的讨论。为此,我们将向这些表中填入一些记录,方法是使用SQL命令,具体如下所示:

INSERT INTO blogs (id, title, content, author) VALUES (NULL,'Title of the first blog entry', 'Content of the first blog entry', 'Ian')

INSERT INTO comments (id, blog_id, comment, author) VALUES (NULL, 1, 'Commenting first blog entry', 'Susan Norton'), (NULL, 1, 'Commenting first blog entry', 'Rose Wilson')

上面的代码,实际上模拟了读者Susan和Rose对我们的第一篇博客作出了评论的情况。假设现在我们要用另一篇文章来更新第一篇博客。当然,这种情况是有可能发生的。

在这种情况下,为了维护数据库的一致性,comments表也必须进行相应的更新,要么通过手工方式更新,或者通过处理数据层的应用程序进行更新。就本例而言,我们将使用SQL命令来完成更新,具体如下所示:

UPDATE blogs SET id = 2, title = 'Title of the first blog entry', content = 'Content of the first blog entry', author = 'John Doe' WHERE id = 1

UPDATE comments SET blog_id = 2 WHERE blod_id = 1

如前所述,因为第一篇博客的数据项的内容已经更新,所以comments表也必须反映出此变化才行。当然,现实中这个更新操作应该在应用程序层完成,而非手工进行,这就意味着这个逻辑必须使用服务器端语言来实现。

为了完成这个操作,对于PHP来说可以通过一个简单的子过程即可,但是实际上,如果使用了外键约束的话,对comments表的更新操作完全可以委托给数据库。

就像文章前面所说的那样,InnoDB MySQL表对这个功能提供了无缝地支持。所以,后面部分我们会使用外键约束重新前面的示例代码。

数据库的级联更新

下面,我们将利用外键约束和InnoDB表(而非默认的MyISAM类型)来重新构建前面的示例代码。为此,首先要重新定义这两个示例表,以便它们可以使用特定的数据库引擎。为此,可以使用如下所示的SQL代码:

OP TABLE IF EXISTS `test`.`blogs`;

CREATE TABLE `test`.`blogs` (

`id` INT(10) UNSIGNED AUTO_INCREMENT,

`title` TEXT,

`content` TEXT,

篇10:Windows中使用Apche并查看MySQL数据库

我的环境是 Sempron3300+ 512内存

软件环境是 windows 2003server企业版sp1+Apache HTTP Server 2.0+mysql5+php5+mysql5

我直接是将Discuz!EXP的一个软件包,( 最大程度提高Discuz!EXP的兼容性,计算机中同时也许正在运行其他MySQL,Discuz!EXP中的MySQL使用6033端口启动服务以避免与您计算机中运行的)在Windows下直接可配置出PHP的运行环境.便配置简单了不少.装上后,可直接配置出上面的环境.(注意要停止IIS服务,以免端口相同而引起冲突.)在安装的时,端口号是可以改变的,如果你不用IIS的话,可直接使用80端口,已符合HTTP的默认端口,我直接使用的80端口.里面还有相关的设置数据库名称的设置,按设置一步步完成即可.装完了,在IE中输入localhost检测是否安装成功.

下面在CMD命令下查看Mysql数据库

开始->运行 中输入cmd

在命令行中找到Discuz!EXP安装所在的目录,我是安装在D盘.(应用程序都不装在C盘)

必依次输入以下命令便可以查看Mysql数据库了.

cd ”Discuz!EXP"

cd MySQL5

cd bin

mysql -uroot -p

show database;

use (你的在配置Discuz!EXP时为网站所设置的数据库的名字)

show tables;

【如何关闭MySQL数据库中错误提示音】相关文章:

1.如何保护MySQL 中的重要数据数据库教程

2.在Linux/Unix平台下安装MySQL数据库

3.linux下迁移mysql数据库存放目录linux操作系统

4.Ubuntu Server下MySql数据库备份脚本代码

5.Linux VPS/服务器上轻松导入、导出MySQL数据库linux服务器应用

6.windows下计划任务自动备份mysql数据库方法Windows服务器操作系统

7.英文简历中的致命错误

8.生活中的错误作文

9.《错误》

10.MySQL内置函数中的日期和时间函数详解

下载word文档
《如何关闭MySQL数据库中错误提示音.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度: 评级1星 评级2星 评级3星 评级4星 评级5星
点击下载文档

文档为doc格式

  • 返回顶部