Python MySQL长查询插入或更新

时间:2017-03-23 00:46:54

标签: python mysql wordpress wordpress-theming

m trying to insert / update a very long sql, tried to format to string, but won对MySQL表格产生任何影响:

import MySQLdb

db = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
cur = db.cursor()

str1 = 'x'
str2 = 'y'


s = """a:150:{i:0;b:0;s:12:"social_icons";a:9:{s:8:"facebook";s:1:"1";s:7:"twitter";s:1:"1";s:5:"email";s:1:"1";s:9:"pinterest";s:1:"1";s:10:"googleplus";s:1:"1";s:8:"linkedin";s:1:"0";s:2:"vk";s:1:"0";s:6:"tumblr";s:1:"0";s:8:"whatsapp";s:1:"0";}s:7:"backups";N;s:9:"smof_init";s:31:"Tue, 16 Aug 2016 23:03:59
+0000";s:17:"minified_flatsome";i:0;s:16:"flatsome_builder";i:1;s:13:"flatsome_docs";i:1;s:16:"maintenance_mode";i:0;s:21:"maintenance_mode_text";s:24:"Please check back soon..";s:19:"html_scripts_header";s:0:"";s:19:"html_scripts_footer";s:0:"";s:15:"html_custom_css";s:6:"div {}";s:22:"html_custom_css_mobile";s:0:"";s:9:"site_logo";s:57:"[site_url]/wp-content/uploads/2016/08/Logomakr_8dvMrU.png";s:0:"";s:165:"Favicon upload has moved to: <br/> <a href=''[site_url]/wp-admin/customize.php?&autofocus%5Bpanel%5Dof-option-logoandicons''>Appearance
> Customize > Site Identity</a>";s:16:"custom_cart_icon";s:0:"";s:14:"site_logo_dark";s:0:"";s:16:"site_logo_sticky";s:0:"";s:11:"body_layout";s:10:"full-width";s:10:"box_shadow";i:0;s:7:"body_bg";s:0:"";s:13:"body_bg_image";s:0:"";s:12:"body_bg_type";s:12:"bg-full-size";s:13:"content_color";s:5:"light";s:10:"content_bg";s:4:"#FFF";s:13:"header_preset";s:0:"";s:13:"header_height";s:3:"120";s:10:"logo_width";s:3:"210";s:13:"logo_position";s:6:"center";s:10:"search_pos";s:4:"left";s:12:"nav_position";s:3:"top";s:8:"nav_size";s:3:"80%";s:18:"myaccount_dropdown";i:1;s:19:"account_login_style";s:4:"link";s:9:"show_cart";i:1;s:14:"top_right_text";s:0:"";s:13:"header_sticky";i:1;s:20:"header_height_sticky";s:2:"70";s:12:"header_color";s:5:"light";s:9:"header_bg";s:4:"#fff";s:13:"header_bg_img";s:0:"";s:17:"header_bg_img_pos";s:8:"repeat-x";s:11:"topbar_show";i:1;s:9:"topbar_bg";s:0:"";s:11:"topbar_left";s:0:"";s:12:"topbar_right";s:0:"";s:15:"nav_position_bg";s:4:"#eee";s:18:"nav_position_color";s:5:"light";s:17:"nav_position_text";s:0:"";s:21:"nav_position_text_top";s:0:"";s:17:"html_after_header";s:0:"";s:10:"html_intro";s:0:"";s:16:"footer_left_text";s:158:"Copyright [ux_current_year] © <strong>asd.com</strong>.<br> <img src=''http://www.aasd.com/wp-content/uploads/2015/12/creditcard-icons.png''>";s:17:"footer_right_text";s:137:"<img src="http://gninggel.com/wp-content/uploads/2016/08/comodo_secure_seal_100x85_transp.png"><br>  %s <br>%s";s:14:"footer_1_color";s:5:"light";s:17:"footer_1_bg_color";s:7:"#ff3233";s:17:"footer_1_bg_image";s:0:"";s:16:"footer_1_columns";s:7:"large-3";s:14:"footer_2_color";s:4:"dark";s:17:"footer_2_bg_color";s:7:"#772222";s:17:"footer_2_bg_image";s:0:"";s:16:"footer_2_columns";s:7:"large-3";s:19:"footer_bottom_style";s:4:"dark";s:19:"footer_bottom_color";s:4:"#333";s:18:"html_before_footer";s:0:"";s:17:"html_after_footer";s:0:"";s:13:"disable_fonts";i:0;s:13:"type_headings";s:4:"Lato";s:10:"type_texts";s:4:"Lato";s:8:"type_nav";s:4:"Lato";s:8:"type_alt";s:14:"Dancing Script";s:11:"type_subset";a:7:{s:5:"latin";s:1:"1";s:12:"cyrillic-ext";s:1:"0";s:9:"greek-ext";s:1:"0";s:5:"greek";s:1:"0";s:10:"vietnamese";s:1:"0";s:9:"latin-ext";s:1:"0";s:8:"cyrillic";s:1:"0";}s:11:"custom_font";s:0:"";s:13:"color_primary";s:7:"#dd3333";s:15:"color_secondary";s:7:"#eeee22";s:13:"color_success";s:7:"#7a9c59";s:11:"color_links";s:0:"";s:13:"button_radius";s:3:"0px";s:15:"dropdown_border";s:0:"";s:11:"dropdown_bg";s:0:"";s:13:"dropdown_text";s:5:"light";s:11:"blog_layout";s:13:"right-sidebar";s:10:"blog_style";s:11:"blog-normal";s:18:"blog_archive_title";i:1;s:11:"blog_header";s:1:" ";s:15:"blog_after_post";s:1:" ";s:16:"blog_post_layout";s:13:"right-sidebar";s:15:"blog_post_style";s:7:"default";s:15:"blog_author_box";i:1;s:10:"blog_share";i:0;s:13:"blog_parallax";i:0;s:19:"featured_items_page";i:0;s:22:"featured_items_pr_page";s:2:"12";s:22:"featured_items_related";s:7:"default";s:29:"featured_items_related_height";s:5:"250px";s:16:"wc_account_links";i:1;s:14:"facebook_login";i:0;s:17:"facebook_login_bg";s:0:"";s:14:"color_checkout";s:0:"";s:10:"color_sale";s:0:"";s:16:"color_new_bubble";s:7:"#7a9c59";s:12:"color_review";s:0:"";s:15:"product_sidebar";s:12:"left_sidebar";s:25:"product_offcanvas_sidebar";s:1:"0";s:15:"product_display";s:4:"tabs";s:18:"cart_dropdown_show";s:1:"1";s:16:"shop_aside_title";s:0:"";s:12:"product_zoom";s:1:"0";s:16:"related_products";s:6:"slider";s:23:"related_products_pr_row";s:1:"4";s:20:"max_related_products";s:2:"12";s:15:"disable_reviews";s:1:"0";s:9:"tab_title";s:0:"";s:11:"tab_content";s:0:"";s:23:"html_before_add_to_cart";s:1:" ";s:22:"html_after_add_to_cart";s:0:"";s:14:"html_shop_page";s:0:"";s:16:"category_sidebar";s:12:"left-sidebar";s:10:"grid_style";s:5:"grid1";s:10:"grid_frame";s:6:"normal";s:12:"masonry_grid";s:1:"0";s:16:"add_to_cart_icon";s:7:"disable";s:25:"short_description_in_grid";s:1:"0";s:9:"cat_style";s:10:"text-badge";s:15:"breadcrumb_size";s:17:"breadcrumb-normal";s:15:"breadcrumb_home";s:1:"1";s:18:"category_row_count";s:1:"3";s:25:"category_row_count_mobile";s:1:"2";s:16:"products_pr_page";s:2:"12";s:13:"search_result";s:1:"0";s:13:"product_hover";s:12:"fade_in_back";s:12:"bubble_style";s:6:"style1";s:22:"sale_bubble_percentage";s:1:"0";s:18:"disable_quick_view";s:1:"0";s:13:"wishlist_icon";s:5:"heart";s:15:"coupon_checkout";s:1:"0";s:17:"continue_shopping";s:1:"0";s:16:"html_cart_footer";s:0:"";s:21:"html_checkout_sidebar";s:0:"";s:14:"html_thank_you";s:0:"";s:12:"catalog_mode";s:1:"0";s:19:"catalog_mode_prices";s:1:"0";s:19:"catalog_mode_header";s:0:"";s:20:"catalog_mode_product";s:0:"";s:21:"catalog_mode_lightbox";s:0:"";s:19:"facebook_login_text";s:0:"";s:23:"facebook_login_checkout";s:1:"1";s:18:"custom_share_icons";s:0:"";s:18:"nav_menu_locations";a:3:{s:7:"primary";i:11;s:14:"primary_mobile";i:11;s:6:"footer";i:12;}s:18:"custom_css_post_id";i:-1;}
""" % (str1, str2)

cur.execute("INSERT INTO `wp_options`(`option_id`, `option_name`, `option_value`, `autoload`) VALUES ('450', 'theme_mods_flatsome', '%s', 'yes')" % s)

db.commit()

也尝试用()代替&#34;&#34;&#34;和&#34;&#34;&#34;,但没有。

没有错误消息,似乎查询没问题,但不会改变任何内容。

制作了一个新文件,尝试从文件中加载字符串

with open('config') as f:
      query = f.read()

cur.execute("UPDATE wp_options SET option_value = '%s' WHERE option_id = 450" % query)

1 个答案:

答案 0 :(得分:0)

问题在于编码,我有几个小时的头痛原因,但我回来分享决议:

str = unicode(str,errors ='ignore')