{"id":825,"date":"2023-03-03T22:38:16","date_gmt":"2023-03-03T14:38:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=825"},"modified":"2023-04-29T16:51:36","modified_gmt":"2023-04-29T08:51:36","slug":"robot-framework-connect-to-mysql-database-operation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/03\/robot-framework-connect-to-mysql-database-operation\/","title":{"rendered":"Robot Framework\u8fde\u63a5MySQL\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"<h2>\u5b89\u88c5robotframework-databaselibrary\u5e93<\/h2>\n<ul>\n<li><strong>robotframework-databaselibrary<\/strong><\/li>\n<\/ul>\n<p>DatabaseLibrary\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/pypi.python.org\/pypi\/robotframework-databaselibrary\">https:\/\/pypi.python.org\/pypi\/robotframework-databaselibrary<\/a><\/p>\n<p><!-- more --><\/p>\n<pre><code>$ pip install robotframework-databaselibrary<\/code><\/pre>\n<ul>\n<li><strong>PyMySQL<\/strong><\/li>\n<\/ul>\n<p>PyMySQL: <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/pypi.python.org\/pypi\/PyMySQL\">https:\/\/pypi.python.org\/pypi\/PyMySQL<\/a><\/p>\n<pre><code>> pip install PyMySQL<\/code><\/pre>\n<ul>\n<li><strong>MySQL-python<\/strong><\/li>\n<\/ul>\n<p>Win64\u5b89\u88c5MySQL-python\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.appblog.cn\/2018\/04\/21\/Win64\u5b89\u88c5MySQL-python\/\">http:\/\/www.appblog.cn\/2018\/04\/21\/Win64\u5b89\u88c5MySQL-python\/<\/a><\/p>\n<h2>Robot\u6d4b\u8bd5\u811a\u672c<\/h2>\n<p>\u4f7f\u7528\u5173\u952e\u5b57\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c<\/p>\n<ul>\n<li><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong>\uff1a<code>Connect to database using custom params<\/code><\/li>\n<\/ul>\n<p>\u8be5\u5173\u952e\u5b57\u9700\u8981\u4f20\u51652\u4e2a\u53c2\u6570\uff0cpymysql\u3001\u53ca\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f(host=&#8217;\u6570\u636e\u5e93\u670d\u52a1\u5668IP&#8217;,port=&#8217;\u6570\u636e\u5e93\u7aef\u53e3&#8217;,user=&#8217;\u6570\u636e\u5e93\u767b\u9646\u7528\u6237\u540d&#8217;,passwd=&#8217;\u5bc6\u7801&#8217;\uff0cdb=&#8217;\u6570\u636e\u5e93\u540d&#8217;)<\/p>\n<ul>\n<li>\n<p><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong>\uff1a<code>Query<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884c\u589e\u5220\u6539\u8bed\u53e5<\/strong>\uff1a<code>Execute Sql String<\/code><\/p>\n<\/li>\n<li>\n<p><strong>\u65ad\u5f00\u6570\u636e\u5e93\u8fde\u63a5<\/strong>\uff1a<code>Disconnect From Database<\/code><\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\u610f\uff1aSQL\u8bed\u53e5\u7ed3\u5c3e\u5c3d\u91cf\u4e0d\u8981\u7528\u5206\u53f7\uff0c\u6bcf\u6b21\u8fde\u63a5\u6570\u636e\u5e93\u540e\u90fd\u4f7f\u7528\u65ad\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u7ed3\u675f<\/p>\n<\/blockquote>\n<pre><code>*** Settings ***\nLibrary           DatabaseLibrary\nLibrary           Collections\n\n*** Test Cases ***\nMySQL\n    Connect to database using custom params    pymysql    host=&#039;127.0.0.1&#039;,port=3306,user=&#039;root&#039;,passwd=&#039;******&#039;,db=&#039;test&#039;,charset=&#039;UTF8&#039;\n    # \u67e5\u8be2\u591a\u6761\n    ${users}    query    SELECT * FROM user\n    ${count}    Get Length    ${users}\n    : FOR    ${i}    IN RANGE    ${count}\n    \\    log    ${i}\n    \\    ${user}    Get From Dictionary    ${users}    ${i}\n    \\    log    ${user}\n    \\    ${name}    Get From Dictionary    ${user}    ${2}\n    # \u67e5\u8be2\u5355\u6761\n    ${user}    query    SELECT * FROM user WHERE username=&#039;yezhou&#039;\n    log    ${user[0][2]}\n    # \u67e5\u8be2\u5355\u6761\uff08\u6307\u5b9a\u5b57\u6bb5\uff09\n    ${name}    query    SELECT name FROM user WHERE username=&#039;yezhou&#039;\n    log    ${name}\n    # \u63d2\u5165\u6570\u636e\n    Execute Sql String    INSERT INTO user(`username`, `name`, `age`, `birthday`) VALUES(&#039;test&#039;, &#039;\u6d4b\u8bd5&#039;, 20, &#039;2000-01-01&#039;)\n    # \u66f4\u65b0\u6570\u636e\n    Execute Sql String    UPDATE user SET `name`=&#039;\u4f60\u597d&#039; WHERE `id`=3\n    # count\u67e5\u8be2\n    ${count}    query    SELECT count(*) FROM user\n    log    ${count}\n    # \u5220\u9664\u6570\u636e\n    Execute Sql String    DELETE FROM user WHERE `id`=3\n    Disconnect From Database<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5robotframework-databaselibrary\u5e93 robotframework-databa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[228],"tags":[20],"class_list":["post-825","post","type-post","status-publish","format-standard","hentry","category-robot-framework","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=825"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/825\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}