How to query a Google Cloud SQL Database and display results as html? - sql

I have a Google Cloud SQL Database. I want to run a static SQL query on the database and display results as html page without using a third party web server.
Any suggestions?

You can query your Cloud SQL database just like you would any MySQL database. you just need to make sure you tell Cloud SQL the IP from which you're going to connect:
or configure your Cloud SQL instance to use a static IP, more about that here.
Then you connect as per normal (this is some PHP taken from here):
$servername = "<The IP address from the overview page of Cloud SQL>";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

Related

Creating a user and login or connect new user to existing sql server login with azure cli command?

I was just wondering if there is a way to create a user and either creating or connect that user to an existing azure sql server login using azure cli?
EDIT:
I would do it with something like this in sql, for example:
-- For login login_name, create a user in the database
CREATE USER <username> FOR LOGIN <login> WITH DEFAULT_SCHEMA = [dbo]
GO
-- Add user to the database owner role
EXEC sp_addrolemember N'db_owner', N'<username>'
GO
You can use below command to run sql query in Azure PowerShell and I followed SO-Thread:
$servername = "sqlservername.database.windows.net"
$databasename = "databasename"
$username = "rithwik"
$pass = "R!th"
$query = "CREATE USER User2 For login xxx WITH DEFAULT_SCHEMA = [dbo]"
$sqlCon = New-Object System.Data.SqlClient.SqlConnection
$sqlCon.ConnectionString = "Server=$servername;Database=$databasename;User ID=$username;Password=$pass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$sqlCon.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand($query, $sqlCon)
$sqlCmd.ExecuteNonQuery()
$sqlCon.Close()
Here xxx is the login name.
Then the user is created:
Then you use
$query = "EXEC sp_addrolemember N'db_or', N'<username>'" in place of above $query to add role.

Databricks online store - Login to Azure SQL Database with Service Principal

I want to use Databricks Online Store with Azure SQL Database, however I am unable to autenthicate through Databricks Feature Store API. I need to use Service Principal credentials.
I tried using Application ID as username and Secret as password ( com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '[REDACTED]'. ClientConnectionId:some-id-x-x-), but no luck.
I also tried to generate AAD access Token and use it as a password, however I am getting password exceeds maximum length of 128characters...
When I use the same credentials to test it via JayDeBeApi everything works...
Code I am using:
from databricks.feature_store.online_store_spec import AzureSqlServerSpec
from databricks.feature_store import FeatureStoreClient
username = "application-id"
password = "application-secret"
tenantId = "tenant-id"
server_name = "server-name.database.windows.net"
port = "1433"
db_name = "database-name"
fs = FeatureStoreClient()
online_store = AzureSqlServerSpec(
hostname=server_name,
port=1433,
database_name=db_name,
user=username,
password=password,
table_name="TableName",
)
fs.publish_table(
name='feature_store.TableName',
online_store=online_store,
mode='merge'
)

Steam API: How to query a server to get server info (not using A2S)?

I would like to know how to use the Steamworks Web API to query a server to get information (server name, game, map, players, etc.). I know that using the A2S query (https://developer.valvesoftware.com/wiki/Server_queries) can give that information, but I would like to know if it is possible using the Steamworks Web API instead.
Thanks!
With IGameServersService/GetServerList. Here is an example:
https://api.steampowered.com/IGameServersService/GetServerList/v1/?key=API_KEY&filter=addr\YOUR.IP.ADRESS:PORT
For example, to get the map of a Garry's Mod server, you can try the following (code example is in PHP):
$url = "https://api.steampowered.com/IGameServersService/GetServerList/v1/?key=API_KEY&filter=addr\185.254.99.6:27015";
$json = file_get_contents($url);
$table2 = json_decode($json, true);
$table = $table2["response"]["servers"][0];
$mapname = $table['map'];

How to put a User Id that's an email address in a connection string

I'm trying to connection to a SQL Azure db using a sys.sysusers with name equal to "myname#mydoman". The issue is that when try with the connection string
Data Source=tcp:coolsqldb.database.windows.net,1433;Initial Catalog=Cool;Persist Security Info=False;User ID=myname#mydoman.com;...
I get the exception
Cannot open server "mydomain.com" requested by the login. The login failed.
at System.Data.SqlClient.SqlIn....
I've tried using quotes in ID=myname#mydoman.com so that it's ID='myname#mydoman.com', but no luck. Any idea what I can do?
If you create those identities on Azure Active Directory you can then create them as a user of an Azure SQL database.
CREATE USER [myname#mydoman.com] FROM EXTERNAL PROVIDER;
Your string connection will then have to change as shown below:
string ConnectionString =
#"Data Source=n9lxnyuzhv.database.windows.net; Authentication=Active Directory Password; Initial Catalog=testdb; UID=myname#mydoman.com; PWD=MyPassWord!";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
For more information, please visit this following documentation.

Access 2010 linked table check

I am stumped. I have a database with two back ends. One is local on a network and the other is over the Internet. When my database is started the user must pass through a logon and once this happens the main form is opened. At this time a query is run to send data from local back end to the external one over the Internet via a linked table. What I am looking to do is check if the linked table has a connection and only run this query if the linked table is connected. The reason for this is that if either the end user or the server the database connects to over the Internet don't have an Internet connection at the time it locks up the users front end. This is no good as this will limit users from using the database if the Internet connection is lost or the server goes down for any such reason
Example: if sql link is ok then
run query
Else goto end
You should be able to use the FileSystemObject to get a suitable folder on the network, if it fails, you do not have a connection.
EDIT
This article uses VBScript to check for SQL Server ports. VBScript runs in VBA with very few changes.
EDIT #2
Powershell can be used to ping remote servers and can be run from VBA
$servers = Get-Content 'servers.txt'
ForEach-Object ($server in $servers) {
# Ping the machine to see if it's on the network
$results = Get-WMIObject -query "select StatusCode from
Win32_PingStatus where Address = '$server'"
$responds = $false
ForEach-Object ($result in $results) {
# If the machine responds break out of the result loop and indicate success
if ($result.statuscode -eq 0) {
$responds = $true
break
}
}
If ($responds) {
# Gather info from the server because it responds
Write-Output "$server responds"
} else {
# Let the user know we couldn't connect to the server
Write-Output "$server does not respond"
}
}
The code above is a cut-and-paste from: http://www.simple-talk.com/sql/database-administration/let-powershell-do-an-inventory-of-your-servers/ by Allen White