Node.js连接MySQL创建表
在本教程中,您将学习如何从node.js应用程序连接到MySQL数据库,并创建一个新表。
要从node.js应用程序连接到MySQL并创建表,请使用以下步骤:
- 连接到MySQL数据库服务器,请参考:http://www.yiibai.com/mysql/nodejs-connect.html
- 调用
connection
对象上的query()
方法来执行CREATE TABLE语句。 - 关闭数据库连接。
以下示例(query.js)显示如何连接到todoapp
数据库并执行CREATE TABLE
语句:
let mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'todoapp'
});
// connect to the MySQL server
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
let createTodos = `create table if not exists todos(
id int primary key auto_increment,
title varchar(255)not null,
completed tinyint(1) not null default 0
)`;
connection.query(createTodos, function(err, results, fields) {
if (err) {
console.log(err.message);
}
});
connection.end(function(err) {
if (err) {
return console.log(err.message);
}
});
});
query()
方法接受SQL语句和回调。回调函数有三个参数:
error
:如果语句执行期间发生错误,则存储详细错误results
:包含查询的结果fields
:包含结果字段信息(如果有)
现在,我们来执行上面的query.js
程序:
F:\worksp\mysql\nodejs\nodejs-connect>node query.js
openssl config failed: error:02001003:system library:fopen:No such process
F:\worksp\mysql\nodejs\nodejs-connect>
查询执行成功,无错误。
我们来看一下在数据库(todoapp
)中是否有成功创建了todos
表:
mysql> USE todoapp;
Database changed
mysql> SHOW TABLES;
+-------------------+
| Tables_in_todoapp |
+-------------------+
| todos |
+-------------------+
1 row in set
mysql>
可以看到,在todoapp
数据库中已经成功地创建了todos
表。
在本教程中,您已经学会了如何在MySQL数据库中创建一个新表。
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:”教程” 选择相关教程阅读或直接访问:http://m.yiibai.com 。
上一篇:MySQL+Python连接和操作
下一篇:哥,这回真没有了
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。
- Java技术群: 227270512 (人数:2000,免费:否)
- MySQL/SQL群: 418407075 (人数:2000,免费:否)
- 大数据开发群: 655154550 (人数:2000,免费:否)
- Python技术群: 287904175 (人数:2000,免费:否)
- 人工智能深度学习: 456236082 (人数:2000,免费:否)
- 测试工程师(新群): 415553199 (人数:1000,免费:否)
- 前端技术群(新群): 410430016 (人数:1000,免费:是)
- C/C++技术(新群): 629264796 (人数:1000,免费:是)
- Node.js技术(新群): 621549808 (人数:1000,免费:是)
- PostgreSQL数据库(新群): 539504187 (人数:1000,免费:是)
- Linux技术: 479429477 (人数:2000,免费:否)
- PHP开发者: 460153241 (人数:2000,免费:否)
- Oracle数据库: 175248146 (人数:2000,免费:否)
- C#/ASP.Net开发者: 579821706 (人数:2000,免费:是)
- 数据分析师: 397883996 (人数:2000,免费:是)R语言,Matlab语言等技术
escitalopram 20mg tablet lexapro 20mg brand revia brand
how to get furosemide without a prescription purchase albuterol generic allergy drugs list
buy strattera 10mg generic strattera 25mg drug order zoloft 100mg sale
ursodiol 150mg for sale buy generic urso cetirizine pills
azithromycin 250mg usa buy zithromax 500mg without prescription buy neurontin 800mg generic
top five acid reflux prescription over counter anti sickness tablets prescription strength gas relief medication
brand deltasone 20mg generic prednisone 5mg brand amoxicillin
free emergency contraception pharmacy how men can last longer best sperm increasing pills
buy phenergan online buy stromectol 3mg sale ivermectin 0.5% lotion
best treatment for duodenal ulcers online urgent care for uti gram positive bacteria causing uti
oral cymbalta 40mg buy glipizide 5mg pill buy modafinil 200mg online cheap
herbs that fight fungal infection drink for high blood pressure blood pressure medication class chart
cyproheptadine ca fluvoxamine us where can i buy nizoral
herpes medication without insurance best meds for pre diabetics new injectable diabetes drugs
provera 10mg ca buy medroxyprogesterone generic hydrochlorothiazide uk
fda approved smoking cessation products conventional disease modifying antirheumatic drugs list of pain medications
online sleep prescriptions hair loss pills boots doctors specializing in weight loss near me
femara price letrozole 2.5mg drug order abilify 20mg without prescription
buy uroxatral 10mg pill best prescription allergy pills treat heartburn without antacids
order minocin online cheap cheap terazosin 5mg cost ropinirole 1mg
dermatologist acne treatment pills oxcarbazepine 600mg drug order trileptal 600mg for sale
buy clonidine 0.1 mg pills buy spiriva 9mcg online cheap buy tiotropium bromide 9mcg pill
buy calcitriol generic buy rocaltrol 0.25 mg pill buy generic fenofibrate over the counter
buy trimox paypal buy clarithromycin 500mg online cheap where can i buy biaxin
buy custom research papers online canadian casino real money online slots
thesis writer essay writing on school suprax 100mg ca
aspirin 75 mg pill free spins no deposit win real money best online blackjack
lamisil buy online real money slot machines free casino games
order trazodone 100mg generic order suhagra 50mg generic order generic clindamycin
order generic ceftin order generic bimatoprost buy robaxin pills
buy tadalafil online cheap tadacip online buy buy indocin 50mg without prescription
tamoxifen uk order rhinocort for sale buy symbicort without a prescription
retin gel cheap buy cheap generic avanafil avana 100mg without prescription
buy clindamycin for sale order cleocin pills best ed pills non prescription
purchase lamotrigine online buy cheap generic lamictal buy generic nemazole online
metronidazole 400mg usa buy sulfamethoxazole online cephalexin 500mg cost
order sildenafil 50mg cheap aurogra estradiol buy online
diflucan 100mg price ampicillin antibiotic online purchase baycip pills
hire essay writer edit my paper best essay service
brand flomax 0.4mg order tamsulosin 0.2mg without prescription order zocor 10mg
buy domperidone 10mg pill cheap coreg order generic sumycin 250mg
ranitidine 150mg price order ranitidine without prescription buy celecoxib 100mg sale
buspin price purchase cordarone online cheap cheap amiodarone 100mg
order generic sumatriptan dutasteride for sale dutasteride where to buy
buy zyloprim rosuvastatin usa order rosuvastatin 20mg pill
nexium over the counter generic topiramate topamax cost
how to get azelastine without a prescription buy acyclovir 400mg sale avalide order
purchase famotidine without prescription prograf brand order generic tacrolimus 1mg
xenical cost diltiazem pill buy generic diltiazem 180mg
order coumadin 5mg pill buy metoclopramide paypal metoclopramide 10mg pill
nortriptyline 25 mg tablet methotrexate 10mg price anacin canada
purchase inderal buy ibuprofen generic clopidogrel 75mg generic
order alendronate for sale buy generic fosamax buy generic nitrofurantoin
order glimepiride for sale cytotec sale etoricoxib 60mg ca
buy lioresal without prescription buy baclofen 25mg sale purchase toradol sale
cheap loratadine 10mg ramipril us dapoxetine without prescription
purchase lioresal without prescription ketorolac cost buy toradol medication
order aceon 8mg online cheap buy fexofenadine 180mg generic buy generic allegra 120mg
order vardenafil 20mg online buy levitra 10mg sale tizanidine 2mg tablet
buy methylprednisolone 4 mg order medrol generic purchase aristocort generic
order clomiphene 100mg pill isosorbide for sale online order imuran without prescription
best online casinos real money real casino cheap levothroid
cheap amantadine 100 mg order generic tenormin 50mg order avlosulfon 100mg online cheap
roulette casino free spins no deposit ivermectin 3 mg tablets
playing poker online albuterol sale order albuterol 4mg online cheap
pantoprazole for sale online order prinivil sale phenazopyridine ca
real money slot machines sugarhouse casino online furosemide 40mg uk
azithromycin 250mg brand neurontin for sale neurontin over the counter
order atorvastatin 20mg online order atorvastatin 80mg sale cheap norvasc 10mg
absorica over the counter accutane pill buy zithromax 250mg online
purchase cefdinir pills lansoprazole for sale buy lansoprazole 30mg sale
order modafinil sale promethazine brand deltasone ca
buy cenforce generic order aralen 250mg generic brand aralen
order tadalafil 40mg pill cialis 20mg uk viagra 50mg pills for men
order micardis 20mg generic molnunat cheap order molnunat 200mg online cheap
prilosec 10mg oral buy generic singulair 10mg order metoprolol 50mg generic
premarin 600 mg usa buy dostinex generic buy viagra online cheap
purchase betahistine generic buy latanoprost online cheap buy generic benemid
buy zovirax generic order exelon 6mg for sale order exelon 6mg generic
vasotec canada bicalutamide 50mg cost duphalac medication
pyridostigmine 60 mg for sale pyridostigmine uk order maxalt generic
cheap ferrous sulfate ascorbic acid without prescription sotalol 40mg generic
prasugrel 10mg usa purchase thorazine for sale buy detrol cheap
monograph online order colospa 135 mg generic buy pletal 100mg pills
buy fludrocortisone sale fludrocortisone online order imodium 2 mg online cheap
dydrogesterone without prescription buy forxiga 10 mg online cheap order empagliflozin online cheap
melatonin buy online buy melatonin no prescription danazol 100 mg price
dipyridamole generic buy dipyridamole 25mg sale where to buy pravachol without a prescription
aspirin us zovirax drug buy imiquad cream
buy acarbose without prescription buy generic precose online fulvicin 250 mg generic
minoxidil solution buy generic mintop buy generic ed pills
pill ketotifen sinequan generic imipramine 25mg sale
order tadalafil order tadalafil 40mg sildenafil professional
fenofibrate canada order tricor 160mg without prescription tricor cost