最近在做一个将mysql数据转换成sqlite数据的功能,因为要将image的数据切换为blob二进制形式保存,所以我就自己写一个类,从类中,我提取出这个方法来分享。为了以后还能正常使用,所以我使用了PDO方式来处理。
/** * 通过Mysql创建SQlite * $db 数据库 * $tbname 表名 * $is_images 需要保存的图片二进制数组 array("img1","img2","img3")形式,默认为false * return SQL的数组形式 */ function creat_sqlite($db, $tbname, $is_images = false){ $runsql1 = $db->query("SELECT * FROM `$tbname` limit 1"); $fields_rows = $runsql1->columnCount(); $tb_fields_info = array(); $tb_string = ""; for ($i = 0; $i < $fields_rows; $i++) { $dot = ($i > 0) ? "," : ""; $tb_meta = $runsql1->getColumnMeta($i); //如果存在数据且字段存在需要取消的字段中 if($is_images && in_array($tb_meta['name'], $is_images)){ continue; } $tb_string .= $dot.$tb_meta['name']; switch($tb_meta['native_type']){ case "VAR_STRING": $tb_string .= " "."varchar"; break; case "LONG": $tb_string .= " "."integer"; break; case "BLOB": $tb_string .= " "."text"; break; } if(isset($tb_meta['flags'][1]) && ($tb_meta['flags'][1] === "primary_key")){ $tb_string .= " "."PRIMARY KEY"; } if($tb_meta['flags'][0] === "not_null") { $tb_string .= " "."NOT NULL"; } } //创建数据的SQL语句 $creat_tbs['data'] = "CREATE TABLE IF NOT EXISTS data ($tb_string);"; //创建图片的SQL语句 if($is_images) { $tb_images = ''; foreach($is_images as $k => $v){ $di = ($k > 0) ? " , " : " "; $tb_images .= $di.$v." blob NOT NULL"; } $creat_tbs['images'] = "CREATE TABLE IF NOT EXISTS images ( id integer PRIMARY KEY NOT NULL, tid integer NOT NULL,$tb_images);"; } return $creat_tbs; }
demo生成的结果(没设置image)为(我将其转换成了json形式):
{"data":"CREATE TABLE IF NOT EXISTS data (id integer PRIMARY KEY NOT NULL,name varchar NOT NULL,nickname varchar NOT NULL,emails varchar NOT NULL,passwd varchar NOT NULL,status NOT NULL);"} |

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
