Node.js连接MySQL创建表

在本教程中,您将学习如何从node.js应用程序连接到MySQL数据库,并创建一个新表。

要从node.js应用程序连接到MySQL并创建表,请使用以下步骤:

  1. 连接到MySQL数据库服务器,请参考:http://www.yiibai.com/mysql/nodejs-connect.html
  2. 调用connection对象上的query()方法来执行CREATE TABLE语句。
  3. 关闭数据库连接。

以下示例(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语言等技术

94 Responses

  1. Ccoqif说道:

    escitalopram 20mg tablet lexapro 20mg brand revia brand

  2. Kamxgp说道:

    how to get furosemide without a prescription purchase albuterol generic allergy drugs list

  3. Pnidrl说道:

    buy strattera 10mg generic strattera 25mg drug order zoloft 100mg sale

  4. Laonwj说道:

    ursodiol 150mg for sale buy generic urso cetirizine pills

  5. Rxsqrl说道:

    azithromycin 250mg usa buy zithromax 500mg without prescription buy neurontin 800mg generic

  6. Bzoklj说道:

    top five acid reflux prescription over counter anti sickness tablets prescription strength gas relief medication

  7. Pilyud说道:

    brand deltasone 20mg generic prednisone 5mg brand amoxicillin

  8. Rdjumy说道:

    free emergency contraception pharmacy how men can last longer best sperm increasing pills

  9. Xvhdcr说道:

    buy phenergan online buy stromectol 3mg sale ivermectin 0.5% lotion

  10. Bwecnh说道:

    best treatment for duodenal ulcers online urgent care for uti gram positive bacteria causing uti

  11. Abcifz说道:

    oral cymbalta 40mg buy glipizide 5mg pill buy modafinil 200mg online cheap

  12. Cxeqta说道:

    herbs that fight fungal infection drink for high blood pressure blood pressure medication class chart

  13. Xrojtc说道:

    cyproheptadine ca fluvoxamine us where can i buy nizoral

  14. Jewiis说道:

    herpes medication without insurance best meds for pre diabetics new injectable diabetes drugs

  15. Uwnvdd说道:

    provera 10mg ca buy medroxyprogesterone generic hydrochlorothiazide uk

  16. Ctnjzd说道:

    fda approved smoking cessation products conventional disease modifying antirheumatic drugs list of pain medications

  17. Uupnvx说道:

    online sleep prescriptions hair loss pills boots doctors specializing in weight loss near me

  18. Tdocqt说道:

    femara price letrozole 2.5mg drug order abilify 20mg without prescription

  19. Rdddzk说道:

    buy uroxatral 10mg pill best prescription allergy pills treat heartburn without antacids

  20. Mloxlm说道:

    order minocin online cheap cheap terazosin 5mg cost ropinirole 1mg

  21. Fnlatc说道:

    dermatologist acne treatment pills oxcarbazepine 600mg drug order trileptal 600mg for sale

  22. Qjhjqk说道:

    buy clonidine 0.1 mg pills buy spiriva 9mcg online cheap buy tiotropium bromide 9mcg pill

  23. Eokrdp说道:

    buy calcitriol generic buy rocaltrol 0.25 mg pill buy generic fenofibrate over the counter

  24. Gianng说道:

    buy trimox paypal buy clarithromycin 500mg online cheap where can i buy biaxin

  25. Vwqptv说道:

    buy custom research papers online canadian casino real money online slots

  26. Ucmqeh说道:

    thesis writer essay writing on school suprax 100mg ca

  27. Kfatsh说道:

    aspirin 75 mg pill free spins no deposit win real money best online blackjack

  28. Nsjjqo说道:

    lamisil buy online real money slot machines free casino games

  29. Qjnbwe说道:

    order trazodone 100mg generic order suhagra 50mg generic order generic clindamycin

  30. Cqlcrw说道:

    order generic ceftin order generic bimatoprost buy robaxin pills

  31. Rhjzmy说道:

    buy tadalafil online cheap tadacip online buy buy indocin 50mg without prescription

  32. Oxnysa说道:

    tamoxifen uk order rhinocort for sale buy symbicort without a prescription

  33. Bmirqr说道:

    retin gel cheap buy cheap generic avanafil avana 100mg without prescription

  34. Dgcjuv说道:

    buy clindamycin for sale order cleocin pills best ed pills non prescription

  35. Njzayj说道:

    purchase lamotrigine online buy cheap generic lamictal buy generic nemazole online

  36. Lgfyvj说道:

    metronidazole 400mg usa buy sulfamethoxazole online cephalexin 500mg cost

  37. Oqtpxi说道:

    order sildenafil 50mg cheap aurogra estradiol buy online

  38. Zudpqf说道:

    diflucan 100mg price ampicillin antibiotic online purchase baycip pills

  39. Gnsisd说道:

    hire essay writer edit my paper best essay service

  40. Dqwcds说道:

    brand flomax 0.4mg order tamsulosin 0.2mg without prescription order zocor 10mg

  41. Volvdf说道:

    buy domperidone 10mg pill cheap coreg order generic sumycin 250mg

  42. Taexpz说道:

    ranitidine 150mg price order ranitidine without prescription buy celecoxib 100mg sale

  43. Lrpczi说道:

    buspin price purchase cordarone online cheap cheap amiodarone 100mg

  44. Lennar说道:

    order generic sumatriptan dutasteride for sale dutasteride where to buy

  45. Nxygzq说道:

    buy zyloprim rosuvastatin usa order rosuvastatin 20mg pill

  46. Wzoiax说道:

    nexium over the counter generic topiramate topamax cost

  47. Zzbzee说道:

    how to get azelastine without a prescription buy acyclovir 400mg sale avalide order

  48. Semsxu说道:

    purchase famotidine without prescription prograf brand order generic tacrolimus 1mg

  49. Hxnfzp说道:

    xenical cost diltiazem pill buy generic diltiazem 180mg

  50. Ycakrz说道:

    order coumadin 5mg pill buy metoclopramide paypal metoclopramide 10mg pill

  51. Szhoer说道:

    nortriptyline 25 mg tablet methotrexate 10mg price anacin canada

  52. Oexdci说道:

    purchase inderal buy ibuprofen generic clopidogrel 75mg generic

  53. Hjkthx说道:

    order alendronate for sale buy generic fosamax buy generic nitrofurantoin

  54. Pnpivq说道:

    order glimepiride for sale cytotec sale etoricoxib 60mg ca

  55. Qvcysw说道:

    buy lioresal without prescription buy baclofen 25mg sale purchase toradol sale

  56. Nkqcau说道:

    cheap loratadine 10mg ramipril us dapoxetine without prescription

  57. Kawzyh说道:

    purchase lioresal without prescription ketorolac cost buy toradol medication

  58. Mibymi说道:

    order aceon 8mg online cheap buy fexofenadine 180mg generic buy generic allegra 120mg

  59. Sbnudm说道:

    order vardenafil 20mg online buy levitra 10mg sale tizanidine 2mg tablet

  60. Dqbwfd说道:

    buy methylprednisolone 4 mg order medrol generic purchase aristocort generic

  61. Gftekx说道:

    order clomiphene 100mg pill isosorbide for sale online order imuran without prescription

  62. Pwciex说道:

    best online casinos real money real casino cheap levothroid

  63. Ftpynl说道:

    cheap amantadine 100 mg order generic tenormin 50mg order avlosulfon 100mg online cheap

  64. Iavwqj说道:

    roulette casino free spins no deposit ivermectin 3 mg tablets

  65. Wwcbdj说道:

    playing poker online albuterol sale order albuterol 4mg online cheap

  66. Cflgnl说道:

    pantoprazole for sale online order prinivil sale phenazopyridine ca

  67. Hfalwe说道:

    real money slot machines sugarhouse casino online furosemide 40mg uk

  68. Mxyafx说道:

    azithromycin 250mg brand neurontin for sale neurontin over the counter

  69. Usmvcb说道:

    order atorvastatin 20mg online order atorvastatin 80mg sale cheap norvasc 10mg

  70. Qntwek说道:

    absorica over the counter accutane pill buy zithromax 250mg online

  71. Ztuwas说道:

    purchase cefdinir pills lansoprazole for sale buy lansoprazole 30mg sale

  72. Gvkpsp说道:

    order modafinil sale promethazine brand deltasone ca

  73. Uwosdj说道:

    buy cenforce generic order aralen 250mg generic brand aralen

  74. Oiylvs说道:

    order tadalafil 40mg pill cialis 20mg uk viagra 50mg pills for men

  75. Atfqrc说道:

    order micardis 20mg generic molnunat cheap order molnunat 200mg online cheap

  76. Rnytfl说道:

    prilosec 10mg oral buy generic singulair 10mg order metoprolol 50mg generic

  77. Hmzmwe说道:

    premarin 600 mg usa buy dostinex generic buy viagra online cheap

  78. Hlopuw说道:

    purchase betahistine generic buy latanoprost online cheap buy generic benemid

  79. Kwrcfx说道:

    buy zovirax generic order exelon 6mg for sale order exelon 6mg generic

  80. Ogesve说道:

    vasotec canada bicalutamide 50mg cost duphalac medication

  81. Czimci说道:

    pyridostigmine 60 mg for sale pyridostigmine uk order maxalt generic

  82. Uhmdnz说道:

    cheap ferrous sulfate ascorbic acid without prescription sotalol 40mg generic

  83. Vwkqqj说道:

    prasugrel 10mg usa purchase thorazine for sale buy detrol cheap

  84. Sylxbz说道:

    monograph online order colospa 135 mg generic buy pletal 100mg pills

  85. Cgufgl说道:

    buy fludrocortisone sale fludrocortisone online order imodium 2 mg online cheap

  86. Aiamvt说道:

    dydrogesterone without prescription buy forxiga 10 mg online cheap order empagliflozin online cheap

  87. Yjwsnk说道:

    melatonin buy online buy melatonin no prescription danazol 100 mg price

  88. Baqavs说道:

    dipyridamole generic buy dipyridamole 25mg sale where to buy pravachol without a prescription

  89. Vfycar说道:

    aspirin us zovirax drug buy imiquad cream

  90. Tuysfy说道:

    buy acarbose without prescription buy generic precose online fulvicin 250 mg generic

  91. Rtqwtk说道:

    minoxidil solution buy generic mintop buy generic ed pills

  92. Gocrag说道:

    pill ketotifen sinequan generic imipramine 25mg sale

  93. Cesveu说道:

    order tadalafil order tadalafil 40mg sildenafil professional

  94. Rmucyr说道:

    fenofibrate canada order tricor 160mg without prescription tricor cost

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

CAPTCHAis initialing...