/[LeafOK_CVS]/lbbs/src/database.c
ViewVC logotype

Diff of /lbbs/src/database.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.8 by sysadm, Mon Apr 28 12:45:57 2025 UTC Revision 1.20 by sysadm, Wed Nov 5 02:06:50 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                   database.c  -  description  /*
3                                                           -------------------   * database
4          begin                : Mon Oct 11 2004   *   - configuration and function of DB connection
5          copyright            : (C) 2004 by Leaflet   *
6          email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
   
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "common.h"  #include "common.h"
10  #include <mysql.h>  #include "database.h"
11    #include "log.h"
12  #include <stdio.h>  #include <stdio.h>
13    #include <mysql/mysql.h>
14    
15  MYSQL *  // Global declaration for database
16  db_open()  char DB_host[DB_host_max_len + 1];
17    char DB_username[DB_username_max_len + 1];
18    char DB_password[DB_password_max_len + 1];
19    char DB_database[DB_database_max_len + 1];
20    char DB_timezone[DB_timezone_max_len + 1];
21    
22    MYSQL *db_open()
23  {  {
24          MYSQL *db;          MYSQL *db = NULL;
25          char sql[1024];          char sql[SQL_BUFFER_LEN];
26    
27          db = mysql_init(NULL);          db = mysql_init(NULL);
28          if (db == NULL)          if (db == NULL)
# Line 32  db_open() Line 31  db_open()
31                  return NULL;                  return NULL;
32          }          }
33    
34          db = mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database,          if (mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database,
35                                                          0, NULL, 0);                                                     0, NULL, 0) == NULL)
         if (db == NULL)  
36          {          {
37                  log_error("mysql_connect() failed\n");                  log_error("mysql_real_connect() error: %s\n", mysql_error(db));
38                    mysql_close(db);
39                  return NULL;                  return NULL;
40          }          }
41    
42          if (mysql_set_character_set(db, "gb2312") != 0)          if (mysql_set_character_set(db, "utf8") != 0)
43          {          {
44                  log_error("Set character set failed\n");                  log_error("Set character set error: %s\n", mysql_error(db));
45                    mysql_close(db);
46                  return NULL;                  return NULL;
47          }          }
48    
49          sprintf(sql,          snprintf(sql, sizeof(sql),
50                  "SET time_zone = '%s'",                           "SET time_zone = '%s'",
51                  DB_timezone);                           DB_timezone);
52    
53          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
54          {          {
55                  log_error("Set timezone failed\n");                  log_error("Set timezone error: %s\n", mysql_error(db));
56                    mysql_close(db);
57                  return NULL;                  return NULL;
58          }          }
59    


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1