[数据库]mysql存储过程的建立及使用

mysql存储过程的建立及使用

建立存储过程,特别是带有参数的情况反复出错,经常提示“字符串格式不正确”,或者是找不到这样,找不到那样,经分细心分析、分步测试,终于上传成功,这里介绍的是APMServ5.2.6:
一,先解决mysql内部错误:
    因为APMServ5.2.6的mysql内部有错,叫BUG好些,容易误入陷阱,需要先把mysql中的proc表删除,然后利用SQL语句重建一个新表,具体参阅本文附件。
二,建一个最简单的存储过程以便测试是否解决了上述BUG总题:
1)================================================
简单测试建立存储过程(不带参数),测试的表自己相应改变
---------------------------------------------------
CREATE PROCEDURE abc()
begin
insert into answertable (answer_subject_id,answer_voteway_id) values (123,456);
end;
注意:在phpMyAdmin界面中Delimiter改为//(或其它)才能操作成功上边语句,因为上边用到了分号,不能再把默认分号作为分割符。
建立存储过程后,在SQL界面测试:call answertable 然后“执行”,
经测试成功,再继续以下测试。
2)==================================
单独建立一个参数(数字类型)的存储过程
-------------------------------------
CREATE PROCEDURE SaveUserAnswer1 (in xanswer_subject_id int(11))
begin
insert into answertable (answer_subject_id) values (xanswer_subject_id);
end
建成存储过程后,C#中调用如下:
数据库操作类中的一个方法:
public void SaveWithProcedure(string _subject)
    {
        MySqlConnection conn = new MySqlConnection(connstr)
        string connstr1 = string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};charset=gb2312;", "127.0.0.1", "3306", "vote", "root", "");
        MySqlDataAdapter mysqldata = new MySqlDataAdapter();
        DataSet dataset = new DataSet();
        conn = new MySqlConnection(connstr1);
        mysqldata.SelectCommand = new MySqlCommand();
        mysqldata.SelectCommand.Connection = conn;
        mysqldata.SelectCommand.CommandText = "SaveUserAnswer1";
        mysqldata.SelectCommand.CommandType = CommandType.StoredProcedure;

        MySqlParameter subject_parameter = new MySqlParameter("?xanswer_subject_id", MySqlDbType.Int32);//mysql的存储过程参数是以?打头的
        subject_parameter.Value  = _subject;
        mysqldata.SelectCommand.Parameters.Add(subject_parameter);

        mysqldata.Fill(dataset, "SaveUserAnswer1");
    }

主界面调用,实例化类(*)后调用类的方法如下:
    *.SaveWithProcedure(103);
3)==================================
相应地,测试字符类型,varchar
------------------------------------
CREATE PROCEDURE SaveUserAnswer2 (in xanswer_ip varchar(20))
begin
insert into answertable (answer_ip) values (xanswer_ip);
end

类的方法改动下边相应字符参数部分,(注意:调用存储名也变更了)
        MySqlParameter ip_parameter = new MySqlParameter("?xanswer_ip", MySqlDbType.VarChar,20 );
        ip_parameter.Value = _ip;
        mysqldata.SelectCommand.Parameters.Add(ip_parameter);
主界面调用,实例化类(*)后调用类的方法:
    *.SaveWithProcedure("192.168.1.3");
4)==================================
相应地,测试时间类型,varchar
------------------------------------
CREATE PROCEDURE SaveUserAnswer3 (in xanswer_time datetime )
begin
insert into answertable (answer_time) values (xanswer_time);
end
类的方法改动下边相应字符参数部分
        MySqlParameter dt_parameter = new MySqlParameter("?xanswer_time", MySqlDbType.Datetime  );
        dt_parameter.Value = _dt;
        mysqldata.SelectCommand.Parameters.Add(dt_parameter);
主界面调用,实例化类(*)后调用类的方法:
    *.SaveWithProcedure(DateTime .Now );
转载请注明出处,欢迎访问:http://www.cnblogs.com/dooroo

---附件(文摘):-------------------------------------------------------
将phpmyadmin中的mysql数据库中的表proc删除
执行以下代码重建该表
------------------------------------------------------------------------
CREATE TABLE `proc` (
`db` char(64) character set utf8 collate utf8_bin NOT NULL default '',
`name` char(64) NOT NULL default '',
`type` enum('FUNCTION','PROCEDURE') NOT NULL,
`specific_name` char(64) NOT NULL default '',
`language` enum('SQL') NOT NULL default 'SQL',
`sql_data_access` enum('CONTAINS_SQL','NO_SQL','READS_SQL_DATA','MODIFIES_SQL_DATA') NOT NULL default 'CONTAINS_SQL',
`is_deterministic` enum('YES','NO') NOT NULL default 'NO',
`security_type` enum('INVOKER','DEFINER') NOT NULL default 'DEFINER',
`param_list` blob NOT NULL,
`returns` char(64) NOT NULL default '',
`body` longblob NOT NULL,
`definer` char(77) character set utf8 collate utf8_bin NOT NULL default '',
`created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`modified` timestamp NOT NULL default '0000-00-00 00:00:00',
`sql_mode` set('REAL_AS_FLOAT','PIPES_AS_CONCAT','ANSI_QUOTES','IGNORE_SPACE','NOT_USED','ONLY_FULL_GROUP_BY','NO_UNSIGNED_SUBTRACTION','NO_DIR_IN_CREATE','POSTGRESQL','ORACLE','MSSQL','DB2','MAXDB','NO_KEY_OPTIONS','NO_TABLE_OPTIONS','NO_FIELD_OPTIONS','GBASE80','GBASE81','ANSI','NO_AUTO_VALUE_ON_ZERO','NO_BACKSLASH_ESCAPES','STRICT_TRANS_TABLES','STRICT_ALL_TABLES','NO_ZERO_IN_DATE','NO_ZERO_DATE','INVALID_DATES','ERROR_FOR_DIVISION_BY_ZERO','TRADITIONAL','NO_AUTO_CREATE_USER','HIGH_NOT_PRECEDENCE') NOT NULL default '',
`comment` char(64) character set utf8 collate utf8_bin NOT NULL default '',
`character_set_client` char(32) character set utf8 collate utf8_bin default NULL,
`collation_connection` char(32) character set utf8 collate utf8_bin default NULL,
`db_collation` char(32) character set utf8 collate utf8_bin default NULL,
`body_utf8` longblob,
PRIMARY KEY (`db`,`name`,`type`)
) DEFAULT CHARSET=utf8 COMMENT='Stored Procedures'

========================================================================================

早期的该表结构如下:

---------------------------------------------------------------------------------------

CREATE TABLE `proc` (
`db` char(64) character set utf8 collate utf8_bin NOT NULL default '',
`name` char(64) NOT NULL default '',
`type` enum('FUNCTION','PROCEDURE') NOT NULL,
`specific_name` char(64) NOT NULL default '',
`language` enum('SQL') NOT NULL default 'SQL',
`sql_data_access` enum('CONTAINS_SQL','NO_SQL','READS_SQL_DATA','MODIFIES_SQL_DATA') NOT NULL default 'CONTAINS_SQL',
`is_deterministic` enum('YES','NO') NOT NULL default 'NO',
`security_type` enum('INVOKER','DEFINER') NOT NULL default 'DEFINER',
`param_list` blob NOT NULL,
`returns` char(64) NOT NULL default '',
`body` longblob NOT NULL,
`definer` char(77) character set utf8 collate utf8_bin NOT NULL default '',
`created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`modified` timestamp NOT NULL default '0000-00-00 00:00:00',
`sql_mode` set('REAL_AS_FLOAT','PIPES_AS_CONCAT','ANSI_QUOTES','IGNORE_SPACE','NOT_USED','ONLY_FULL_GROUP_BY','NO_UNSIGNED_SUBTRACTION','NO_DIR_IN_CREATE','POSTGRESQL','ORACLE','MSSQL','DB2','MAXDB','NO_KEY_OPTIONS','NO_TABLE_OPTIONS','NO_FIELD_OPTIONS','GBASE323','GBASE40','ANSI','NO_AUTO_VALUE_ON_ZERO','NO_BACKSLASH_ESCAPES','STRICT_TRANS_TABLES','STRICT_ALL_TABLES','NO_ZERO_IN_DATE','NO_ZERO_DATE','INVALID_DATES','ERROR_FOR_DIVISION_BY_ZERO','TRADITIONAL','NO_AUTO_CREATE_USER','HIGH_NOT_PRECEDENCE') NOT NULL default '',
`comment` char(64) character set utf8 collate utf8_bin NOT NULL default '',
PRIMARY KEY (`db`,`name`,`type`)
) DEFAULT CHARSET=utf8 COMMENT='Stored Procedures'

posted @ 2012-10-16 10:38  小颗豆  阅读(470)  评论(0编辑  收藏  举报