Python向Mysql写入时间类型数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/libaominshouzhang/article/details/91596455

mysql中字段包括date和datetime两种时间类型,分别介绍如何使用Python向mysql写入上述两种时间类型的数据(主要为sql语句):
1、date类型
date = datetime.datetime.now.strftime("%Y-%m-%d")
sql = “INSERT INTO TABLE_NAME(字段) values(str_to_date(’%s’,’%%Y-%%m-%%d’))”%(date)
2、datetime类型
datetime = datetime.datetime.now.strftime("%Y-%m-%d %H:%M:%S")
sql = “INSERT INTO TABLE_NAME(字段) values(str_to_date(’%s’,’%%Y-%%m-%%d %%H:%%i:%%S’))”%(datetime)

展开阅读全文

mysql写入日期型数据

01-19

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.io.*,java.text.*"%>rnrnrnrnrn<%rntryrn rn Class.forName("org.gjt.mm.mysql.Driver").newInstance();rnrn Connection con=java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1/gold?useUnicode=true&characterEncoding=GB2312","root","123456");rnrn Statement stmt=con.createStatement();rn java.util.Date time=new java.util.Date();rn SimpleDataFormat s=new SimpleDateFormat("yyyy-MM-dd");rn String timeString=s.format(time);rn stmt.execute("update junior set firsttime ='"+timeString+"' where sn =15741");rn rn stmt.close();rn con.close();rn rn catch(Exception e)rn rn e.printStackTrace();rn rn %> rnrnrnrnrn没编译过去。rn这是出错信息:HTTP Status 500 - rnrn--------------------------------------------------------------------------------rnrntype Exception reportrnrnmessage rnrndescription The server encountered an internal error () that prevented it from fulfilling this request.rnrnexception rnrnorg.apache.jasper.JasperException: Unable to compile class for JSPrnrnAn error occurred at line: 6 in the jsp file: /jilushijian.jsprnrnGenerated servlet error:rn [javac] Compiling 1 source filernrnD:\Tomcat 5.0\work\Catalina\localhost\li\org\apache\jsp\jilushijian_jsp.java:53: cannot resolve symbolrnsymbol : class SimpleDataFormat rnlocation: class org.apache.jsp.jilushijian_jsprn SimpleDataFormat s=new SimpleDateFormat("yyyy-MM-dd");rn ^rn1 errorrnrnrn org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)rn org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)rn org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)rn org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)rn org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)rn org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)rn org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)rn org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:856)rnrnrnnote The full stack trace of the root cause is available in the Tomcat logs.rnrnrnrn 论坛

MySQL MediumBlob数据写入

07-09

平台是vs2012rn目前有8个数据要写入,3个为一般的string,int类型,有1个二进制数据。rn不写入二进制数的时候能够很好的写入MySQL,添加了二进制数据后运行就出错,程序会死在mysql_stmt_bind_param(stmt, bind)这里,应该是二进制数据的问题。rnrnMySQL中类型是MediumBlob, 要写入的类型是VARIANT, 是用BYTE* 转换成的VARIANTrnrn刚开始使用MySQL,不太搞得清状况,请大神帮忙rnrnVARIANT的信息:由BYTE* m_pbuffer转化而来rn   varPic.vt=VT_ARRAY|VT_UI1;rn   varPic.parray=safeArray;rnrnrn这边是写入数据库的代码:rn[code=c]rnint add_to_mysql(VARIANT varPic)rnrn MYSQL *conn_ptr;rnrn conn_ptr = mysql_init(NULL);rn conn_ptr = mysql_real_connect(conn_ptr, "127.0.0.1", "root", "123456","picture_info", 3306, NULL, 0);rn rn if (conn_ptr) rn rn MYSQL_STMT *stmt; rn MYSQL_BIND bind[3]; rn memset(bind,0,sizeof(bind));rn stmt = mysql_stmt_init(conn_ptr); rn char* insertSQL="insert into lala values(?, ?, ?)";rn if (mysql_stmt_prepare(stmt, insertSQL, strlen(insertSQL))) rn rn printf("mysql_stmt_prepare(), INSERT failed,%s\r\n",mysql_error(conn_ptr)); rn return -1; rn rn bind[0].buffer_type= MYSQL_TYPE_VAR_STRING; rn bind[0].buffer= "YANA"; rn bind[0].buffer_length= strlen("YANA"); rn rn bind[1].buffer_type= MYSQL_TYPE_VAR_STRING; rn bind[1].buffer= "JPG"; rn bind[1].buffer_length= strlen("JPG");rn /*这里不知道应该怎么写,换了好几种写法都无法顺利运行*/rn bind[2].buffer_type= MYSQL_TYPE_MEDIUM_BLOB; rn bind[2].buffer= varPic.parray; rn bind[2].buffer_length= sizeof(varPic.parray);rn /*到这里为止*/rn if (mysql_stmt_bind_param(stmt, bind)) rn rn printf("mysql_stmt_bind_param() failed %s\r\n", mysql_stmt_error(stmt)); rn return -1;rn rn rn if (mysql_stmt_execute(stmt)) rn rn printf( " mysql_stmt_execute(), failed %s\r\n", mysql_stmt_error(stmt)); rn return -1; rn rn mysql_stmt_close(stmt); rnrn rn mysql_close(conn_ptr);rn return 0;rnrnrn[/code] 论坛

没有更多推荐了,返回首页