{"id":232,"date":"2023-03-31T11:05:54","date_gmt":"2023-03-31T03:05:54","guid":{"rendered":"https:\/\/www.road-trip.cc\/?p=232"},"modified":"2023-03-31T11:05:55","modified_gmt":"2023-03-31T03:05:55","slug":"%e4%bd%bf%e7%94%a8golang%e6%93%8d%e4%bd%9cmysql%e6%95%b0%e6%8d%ae%e5%ba%93%ef%bc%8c%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e9%94%99%e8%af%af%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.road-trip.cc\/?p=232","title":{"rendered":"\u4f7f\u7528golang\u64cd\u4f5cmysql\u6570\u636e\u5e93\uff0c\u600e\u4e48\u83b7\u53d6\u9519\u8bef\u7801"},"content":{"rendered":"\n<p>\u53d1\u73b0\u6709\u4e00\u4e9bgolang\u7684\u9879\u76ee\u4e2d\uff0c\u6709\u901a\u8fc7\u5206\u6790\u6570\u636e\u5e93\u62a5\u51fa\u6765\u7684\u9519\u8bef\u4fe1\u606f\u6765\u8fdb\u884c\u540e\u7eed\u903b\u8f91\u5904\u7406\u7684\u3002\u6bd4\u5982\u901a\u8fc7strings.Contains(err.Error(), &#8220;doesn&#8217;t exist&#8221;)\uff0c\u6765\u5224\u65ad\u67d0\u4e2a\u8868\u4e0d\u5b58\u5728\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u591f\u5f3a\u58ee\uff0c\u6700\u4f73\u65b9\u5f0f\u662f\u901a\u8fc7\u9519\u8bef\u7801\uff0c\u5982\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"database\/sql\"\n\t\"fmt\"\n\n\t\"github.com\/VividCortex\/mysqlerr\"\n\t\"github.com\/go-sql-driver\/mysql\"\n)\n\nvar (\n\tERRNO_TABLE_DOES_NOT_EXIST uint16 = 1146\n)\n\nfunc TagTableExists(table string) bool {\n\n\tdb, err := sql.Open(\"mysql\", \"user:password@tcp(localhost)\/db\")\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\n\tstmtIns, err := db.Prepare(fmt.Sprintf(\"desc %s\", table))\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tdefer stmtIns.Close()\n\n\t_, err = stmtIns.Exec()\n\tif err != nil {\n\t\tif driverError, ok := err.(*mysql.MySQLError); ok {\n\t\t\tif driverError.Number == mysqlerr.ER_NO_SUCH_TABLE {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\tpanic(err.Error())\n\t}\n\treturn true\n}\n\nfunc main() {\n\tfmt.Println(TagTableExists(\"sometable\"))\n}\n<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\u6587\u6863<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/go-sql-driver\/mysql\">GitHub &#8211; go-sql-driver\/mysql: Go MySQL Driver is a MySQL driver for Go&#8217;s (golang) database\/sql package<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/VividCortex\/mysqlerr\">GitHub &#8211; VividCortex\/mysqlerr: MySQL Server Error Constants for Golang<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/go-database-sql.org\/errors.html\">Handling Errors (go-database-sql.org)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u53d1\u73b0\u6709\u4e00\u4e9bgolang\u7684\u9879\u76ee\u4e2d\uff0c\u6709\u901a\u8fc7\u5206\u6790\u6570\u636e\u5e93\u62a5\u51fa\u6765\u7684\u9519\u8bef\u4fe1\u606f\u6765\u8fdb\u884c\u540e\u7eed\u903b\u8f91\u5904\u7406\u7684\u3002\u6bd4\u5982\u901a\u8fc7strings. &#8230; <a title=\"\u4f7f\u7528golang\u64cd\u4f5cmysql\u6570\u636e\u5e93\uff0c\u600e\u4e48\u83b7\u53d6\u9519\u8bef\u7801\" class=\"read-more\" href=\"https:\/\/www.road-trip.cc\/?p=232\" aria-label=\"\u9605\u8bfb \u4f7f\u7528golang\u64cd\u4f5cmysql\u6570\u636e\u5e93\uff0c\u600e\u4e48\u83b7\u53d6\u9519\u8bef\u7801\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[74,14,41,75],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-7","tag-err","tag-golang","tag-mysql","tag-mysqlerr"],"_links":{"self":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=232"}],"version-history":[{"count":1,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}