Пример #1
0
bool DBMysql::connectToDB()
{
    if (0 != mysql_library_init(0, NULL, NULL))
    {
        qDebug() << "MySQL init failed.";
        return false;
    }

    // set charset
    if (0 != mysql_options(&m_dbHandler, MYSQL_SET_CHARSET_NAME, "utf8"))
    {
        qDebug() << "Set charset failed.";
        return false;
    }

    if (NULL == mysql_real_connect( &m_dbHandler,
                                    m_host.toStdString().data(),
                                    m_user.toStdString().data(),
                                    m_pwd.toStdString().data(),
                                    m_dbName.toStdString().data(),
                                    m_port,
                                    NULL,
                                    0 ))
    {
        qDebug() << "Connect to mysql failed:" << mysql_error(&m_dbHandler);
        return false;
    }

    mysql_query(&m_dbHandler, "SET NAMES UTF8;");

    return true;
}
Пример #2
0
CDatabase::CDatabase()
{
	m_uCounter = 0;

	mysql_library_init(0,NULL,NULL);
	CDatabase::Inst() = this;
}
Пример #3
0
int
main (int argc, char *argv[])
{
  MY_INIT (argv[0]);
  /* initialize client library */
  if (mysql_library_init (0, NULL, NULL))
  {
    fprintf (stderr, "mysql_library_init() failed\n");
    exit (1);
  }
  /* initialize connection handler */
  conn = mysql_init (NULL);
  if (conn == NULL)
  {
    fprintf (stderr, "mysql_init() failed (probably out of memory)\n");
    exit (1);
  }
  /* connect to server */
  if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,
      opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
  {
    fprintf (stderr, "mysql_real_connect() failed\n");
    mysql_close (conn);
    exit (1);
  }
  /* disconnect from server, terminate client library */
  mysql_close (conn);
  mysql_library_end ();
  exit (0);
}
Пример #4
0
/* server sampling process */
void local_process(int total_samples) {
    static MYSQL* conn;

    if(mysql_library_init(0, NULL, NULL)) {
        fprintf(stderr, "mysql_library_init() failed\n");
        exit(EXIT_FAILURE);
    } 

    conn = mysql_init(NULL);
    if(conn == NULL) {
        fprintf(stderr, "mysql_init() failed\n");
        exit(EXIT_FAILURE);
    }

    if(mysql_real_connect(conn, my_host_name, my_user_name, my_password, my_db_name, my_port_num, my_socket_name, my_flags) == NULL) {
        fprintf(stderr, "mysql_real_connect() failed:\n%s\n", mysql_error(conn));
        mysql_close(conn);
        exit(EXIT_FAILURE);
    }

    if(mysql_query(conn, "use prototype")) {
        fprintf(stderr, "use database failed: \n%s\n", mysql_error(conn));
        exit(EXIT_FAILURE);
    }

    // execute the outer routine
    prepare_and_exec_serverinsert(conn, total_samples);

    mysql_close(conn);
    mysql_library_end();

    exit(EXIT_SUCCESS);
}
Пример #5
0
bool cMysqlDatabase::Open()
{
    cMutexLock mutexLock(&mutex);
    mysql = mysql_init(NULL);
    if (!mysql)
        return false;

    mysql_library_init(0, NULL, NULL);

    mysql_options (mysql, MYSQL_OPT_CONNECT_TIMEOUT, "5");

    std::string databaseName;
    std::string::size_type idx = path.find_last_of('/');
    if (idx != std::string::npos)
        databaseName = path.substr(idx+1);
    else
        databaseName = path;

    // create the database
    if (!noCreate && !CreateDatabase(databaseName))
        return false;

    MYSQL *m = mysql_real_connect (mysql, "127.0.0.1", "reeluser", "reeluser", databaseName.c_str(), 0, NULL, 0);
    if (!m)
        return false;

    return true;
}
Пример #6
0
int CMySqlAccess::Init(int flag, const char *host, const char *uid, 
			const char *passwd, const char *database)
{
	printf(">>> init begin\n");
	m_iFlag = flag;
	m_strHost = host;
	m_strUid = uid;
	m_strPasswd = passwd;
	m_strDb = database;
	m_res_ptr = NULL;

         if (mysql_library_init(0, NULL, NULL))
        {
                printf("mysql_library_init return failed\n");
                return -1;
        }

        if ((m_mysql = mysql_init(NULL)) == NULL)
        {
                printf("mysql_init return failed, errinfo = %s\n", mysql_error(m_mysql));
                return -1;
        }

        if (mysql_real_connect(m_mysql, host, uid, passwd, database, 0, NULL, 0) == NULL)
        {
                printf("mysql_real_connect return failed, errinfo = %s\n", mysql_error(m_mysql));
                mysql_close(m_mysql);
                return -1;
        }

	printf(">>> init end\n");
	return 0;
}
Пример #7
0
int CMySqlAccess::Reinit()
{
	Uninit();
	mysql_init(m_mysql);
	m_res_ptr = NULL;

        if (mysql_library_init(0, NULL, NULL))
        {
                printf("mysql_library_init return failed\n");
                return -1;
        }

        if ((m_mysql = mysql_init(NULL)) == NULL)
        {
                printf("mysql_init return failed, errinfo = %s\n", mysql_error(m_mysql));
                return -1;
        }

	 MYSQL *connect = mysql_real_connect(m_mysql, m_strHost.c_str(), 
					m_strUid.c_str(), m_strPasswd.c_str(), 
					m_strDb.c_str(), 0, NULL, 0);

	if (connect ==  NULL)
	{
		printf("%s\n", mysql_error(m_mysql));
		mysql_close(m_mysql);
		return -1;
	}

//	printf(">>> init end\n");
	return 0;
}
Пример #8
0
void 
MySQLLibrary::Init( const vector< string > & options )
{
    int numOptions = static_cast< int >( options.size() );
    if ( numOptions == 0 )
        Init( );
    else
    {
        vector< vector< char > > vvOptions( numOptions + 1 );
        vector< char * > vpOptions( numOptions + 1 );
        //The first "argument" is ignored
        vvOptions[ 0 ].resize( 1, 0 );
        vpOptions[ 0 ] = &(vvOptions[ 0 ][ 0 ]);
        for ( int i = 0; i < numOptions; ++i )
        {
            vvOptions[ i + 1 ].resize( options[ i ].size() + 1, 0 );
            vpOptions[ i + 1 ] = &(vvOptions[ i + 1 ][ 0 ]);
            strcpy( vpOptions[ i + 1 ], options[ i ].c_str() );
        }

        int initRslt = mysql_library_init( numOptions, &vpOptions[0], 0 );

        if ( initRslt != 0 )
        {
            string msg( "mysql_library_init returned " );
            msg += IntToString( initRslt );
            MySQLException exception( msg );
            throw exception;
        }
        m_initialized = true;
    }
}
Пример #9
0
static void
dblayer_mysql_initialize(void) {
    if (mysql_library_init(0, NULL, NULL)) {
        fprintf(stderr, "could not initialize MySQL library\n");
        exit(1);
    }
}
Пример #10
0
static GDALDataset *OGRMySQLDriverOpen( GDALOpenInfo* poOpenInfo )

{
    OGRMySQLDataSource     *poDS;

    if( !EQUALN(poOpenInfo->pszFilename,"MYSQL:",6) )
        return NULL;
 
    {
        CPLMutexHolderD(&hMutex);
        if( !bInitialized )
        {
            if ( mysql_library_init( 0, NULL, NULL ) )
            {
                CPLError( CE_Failure, CPLE_AppDefined, "Could not initialize MySQL library" );
                return NULL;
            }
            bInitialized = TRUE;
        }
    }

    poDS = new OGRMySQLDataSource();

    if( !poDS->Open( poOpenInfo->pszFilename, poOpenInfo->eAccess == GA_Update ) )
    {
        delete poDS;
        return NULL;
    }
    else
        return poDS;
}
Пример #11
0
void Init()
{
	//初始化数据库  
	mysql_library_init(0, NULL, NULL);
	//初始化数据结构  
	mysql_init(&mydata);
	mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk");
}
int main(int argc,char * argv [])
{
	if (mysql_library_init(argc, argv, NULL))
		return 0;
	dump_file();
	mysql_library_end();
	return 0;
}
Пример #13
0
void Connector::registerConnector()
{
	if (mysql_library_init(0, 0, 0) != 0)
	{
		throw Exception("mysql_library_init error");
	}

	Poco::Data::SessionFactory::instance().add(KEY, new Connector());
}
DllExport int call_conv driverMySQLEmbedded_lib_init()
{    
  int result;
  result = mysql_library_init(0, NULL, (char **)server_groups);
   
  if ( result==0 ) 
    return TRUE;
  else return FALSE;
}
Пример #15
0
int db_library_init()
{
	if (mysql_library_init (0, NULL, NULL))
	{
		print_error (NULL, "mysql_library_init() failed");
		return -1;
	}
	return 0;
}
Пример #16
0
void
sl_static_init_ext_mysql()
{
    mysql_library_init(0, NULL, NULL);

    cMySQL = sl_vm_store_register_slot();
    cMySQL_Statement = sl_vm_store_register_slot();
    cMySQL_Error = sl_vm_store_register_slot();
}
Пример #17
0
DatabaseWorkerPool::DatabaseWorkerPool() :
m_queue(new ACE_Activation_Queue(new ACE_Message_Queue<ACE_MT_SYNCH>)),
m_connections(0)
{
    m_infoString = "";

    mysql_library_init(-1, NULL, NULL);
    WPFatal (mysql_thread_safe(), "Used MySQL library isn't thread-safe.");
}
Пример #18
0
int main()
{
	AbsMsg* absmsg;
	pthread_t tdatarecv, mysqllocal, mysqlcloud, timer;	//数据获取、本地数据库存储、云数据库存储、数据库定时器线程
	void* threadret;
	pid_t pid;
	int status;

	while(1){
		if((pid = fork()) > 0){
			//主进程,守护进程
			wait(&status);	//阻塞,至子进程退出
			continue;
		}else{
			//子进程,应用程序
			//setsid();//创建新会话
			printf(">>>>>>>>>>>>>>>START!>>>>>>>>>>>>>\n");
			mysql_library_init(0, NULL, NULL);		//mysql 多线程连接初始化

			absmsg = (AbsMsg*)malloc(sizeof(AbsMsg));
			absmsg->NQueue = (struct NodeNeiQueue*)malloc(sizeof(struct NodeNeiQueue));
			absmsg->NQueue->head = 0;
			absmsg->NQueue->tail = 0;
			//互斥锁初始化
			pthread_mutex_init(&absmsg->ecollock1, NULL);
			pthread_mutex_init(&absmsg->ecollock2, NULL);
			//串口数据获取线程
			assert(pthread_create(&tdatarecv, NULL, tdatarecv_fn, absmsg) == 0);
			//本地数据库存储线程
			assert(pthread_create(&mysqllocal, NULL, mysqllocal_fn, absmsg) == 0);
			//云端数据库存储线程,允许失败
			pthread_create(&mysqlcloud, NULL, mysqlcloud_fn, absmsg);
			//定时器线程
			assert(pthread_create(&timer, NULL, timer_fn, NULL) == 0);

			while(1){
				//监控四个线程,有退出则重启
				if(pthread_tryjoin_np(tdatarecv,NULL) == 0)
					assert(pthread_create(&tdatarecv, NULL, tdatarecv_fn, absmsg) == 0);

				if(pthread_tryjoin_np(mysqllocal,NULL) == 0)
					assert(pthread_create(&mysqllocal, NULL, mysqllocal_fn, absmsg) == 0);

				if(pthread_tryjoin_np(mysqlcloud,NULL) == 0)
					pthread_create(&mysqlcloud, NULL, mysqlcloud_fn, absmsg);

				if(pthread_tryjoin_np(timer,NULL) == 0)
					assert(pthread_create(&timer, NULL, timer_fn, NULL) == 0);

				sleep(1);
			}
			

		}
	}

}
Пример #19
0
/*==============================================================================
------------------------------------------------------------------------------*/
bool db_init(
    )
{
    int ret = mysql_library_init(0, NULL, NULL);

    if (ret)
        LOG(LOG_ERR, "could not initialize mysql library");

    return !ret;
}
Пример #20
0
int mysql_drv_init(void)
{
  char *s;
  
  args.hosts = sb_get_value_list("mysql-host");
  if (SB_LIST_IS_EMPTY(args.hosts))
  {
    log_text(LOG_FATAL, "No MySQL hosts specified, aborting");
    return 1;
  }
  hosts_pos = args.hosts;
  pthread_mutex_init(&hosts_mutex, NULL);
  
  args.port = sb_get_value_int("mysql-port");
  args.socket = sb_get_value_string("mysql-socket");
  args.user = sb_get_value_string("mysql-user");
  args.password = sb_get_value_string("mysql-password");
  args.db = sb_get_value_string("mysql-db");
  args.myisam_max_rows = sb_get_value_int("myisam-max-rows");
  args.use_ssl = sb_get_value_flag("mysql-ssl");
  args.create_options = sb_get_value_string("mysql-create-options");
  if (args.create_options == NULL)
    args.create_options = "";
  
  use_ps = 0;
#ifdef HAVE_PS
  mysql_drv_caps.prepared_statements = 1;
  if (db_globals.ps_mode != DB_PS_MODE_DISABLE)
    use_ps = 1;
#endif

  s = sb_get_value_string("mysql-engine-trx");
  if (s == NULL)
  {
    log_text(LOG_FATAL, "--mysql-engine-trx requires an argument");
    return 1;
  }
  if (!strcasecmp(s, "yes"))
    args.engine_trx = ENGINE_TRX_YES;
  else if (!strcasecmp(s, "no"))
    args.engine_trx = ENGINE_TRX_NO;
  else if (!strcasecmp(s, "auto"))
    args.engine_trx = ENGINE_TRX_AUTO;
  else
  {
    log_text(LOG_FATAL, "Invalid value for mysql-engine-trx: %s", s);
    return 1;
  }
  
  s = sb_get_value_string("mysql-table-engine");

  mysql_library_init(0, NULL, NULL);
  
  return parse_table_engine(s);
}
Пример #21
0
int main (int argc, char** argv) {
	mysql_library_init (argc, argv, NULL);
	MYSQL *dbh = getDatabaseHandle("localhost", "round", "round", "round");
	int numCategories;
	Category *categories = getCategories(dbh, &numCategories);
	for (int i = 0; i < numCategories; i++) {
		printf("%d, %s, %s, %f\n",
			categories[i].id, categories[i].name, categories[i].musicuri, categories[i].musicvolume);
	}
	return 0;
}
Пример #22
0
DatabaseMysql::DatabaseMysql()
{
	// before first connection
	if (db_count++ == 0)
	{
		// Mysql Library Init
		mysql_library_init(-1, nullptr, nullptr);

		if (!mysql_thread_safe())
			poco_bugcheck_msg("FATAL ERROR: Used MySQL library isn't thread-safe.");
	}
}
Пример #23
0
int main(int argc, char *argv[]) {
	Server hss_server;
	
	check_server_usage(argc, argv);
	if(mysql_library_init(0, NULL, NULL))
		cout << "ERROR: mysql library cannot be opened" << endl;
	hss_server.begin_thread_pool(atoi(argv[1]), process_req);
	hss_server.fill_server_details(g_hss_port, g_hss_addr);
	hss_server.bind_server();
	hss_server.listen_accept();
	mysql_library_end();
	return 0;
}
Пример #24
0
	database::database() : db_shard_count(0)
	{
		try
		{
			if (mysql_library_init(0, 0, 0) != 0)
			{
				throw std::runtime_error("mysql_library_init failed");
			}
		}
		catch (...)
		{
			throw;
		}
	}
Пример #25
0
int
realapp ()
{
#ifdef WITH_MYSQL
	mysql_library_init(0, NULL, NULL);
#endif
	os_mutex_init (&crypt_lock);
	os_mutex_init (&getpwnam_lock);
	os_mutex_init (&getspnam_lock);
	os_mutex_init (&localtime_lock);
	os_mutex_init (&writerpipe_lock);
#ifndef _WIN32_
	reloadconfig (SIGHUP);
	signal (SIGCHLD, kidkiller);
	signal (SIGPIPE, ignorer);
	signal (SIGQUIT, closeup);
	signal (SIGINT, closeup);
#else
	reloadconfig (0);
#endif
#ifndef _WIN32_
	if (runAsDaemon)
		daemonize ();
	else if (log_filename) {
		int fh = open (log_filename, O_CREAT|O_TRUNC|O_RDWR, 0666);
		if (fh == -1) {
			fprintf(stderr, "failed to open logfile %s:%s\n", log_filename, strerror(errno));
			fh = open ("/dev/null", O_RDWR);
		}

		close (1);
		close (2);
		dup2 (fh, 1);
		dup2 (fh, 2);
	}
#endif
	log_log (NULL, LOG_EVT_SERVERSTART, 0, conf);
	while (TRUE) {
		if (!HandleRequest ()) {
#ifdef WITH_DEBUG
			DEBUG_LOG ("Couldn't handle request.");
#endif
		}
	}

#ifdef WITH_MYSQL
	mysql_library_end();
#endif
	return EXIT_SUCCESS;
}
Пример #26
0
    void init(int argc, char const *serverArgs[], char const *serverGroups[], char const * dbName = 0, char const * user = 0, char const * password = 0) {
#ifndef NDEBUG
        std::cerr << "mysql_library_init\n";
#endif
        if (int ret = mysql_library_init(argc,
                                         const_cast<char **>(serverArgs),
                                         const_cast<char **>(serverGroups)))
        {
            std::cerr << "failed to initialise embedded mysql " << ret << std::endl;
            return;
        }

        init(dbName, user, password);
    }
Пример #27
0
DatabaseMysql::DatabaseMysql() : Database(), mMysql(0)
{
    // before first connection
    if (db_count++ == 0)
    {
        // Mysql Library Init
        mysql_library_init(-1, NULL, NULL);

        if (!mysql_thread_safe())
        {
            sLog.outError("FATAL ERROR: Used MySQL library isn't thread-safe.");
            exit(1);
        }
    }
}
Пример #28
0
int main(int argc, char **argv)
{

  mysql_library_init(0,0,NULL);

  if (argc > 1)
    get_options(argc, argv);

  get_envvars();

  run_tests(my_tests);

  mysql_server_end();
  return(exit_status());
}
Пример #29
0
void 
MySQLLibrary::Init( )
{
    if ( ! m_initialized )
    {
        int initRslt = mysql_library_init( 0, 0, 0 );
        if ( initRslt != 0 )
        {
            string msg( "mysql_library_init returned " );
            msg += IntToString( initRslt );
            MySQLException exception( msg );
            throw exception;
        }
        m_initialized = true;
    }
}
Пример #30
0
DatabaseMysql::DatabaseMysql()
{
    // before first connection
    if (db_count++ == 0)
    {
        // Mysql Library Init
        mysql_library_init(-1, nullptr, nullptr);

        if (!mysql_thread_safe())
        {
            sLog.outError("FATAL ERROR: Used MySQL library isn't thread-safe.");
            Log::WaitBeforeContinueIfNeed();
            exit(1);
        }
    }
}