Error: "Unable to Open Database" using sqite3 and Windows - mysql

I'm a newbie to this, and can't figure out how to solve this error that I get when attempting to query from the database. The error reads:
rm: cannot unlink 'C:/Users/myName/Documents/GitHub/active_record_lite/lib../cats.db': Permission denied
Error: unable to open database "'C:\Users\myName\Documents\GitHub\active_record_lite'": unable to open database file
By going to "Properties" and viewing the "Security" tab, everything is checked under "Allow" for security, so I assume that the project and all subfiles have full permission (is this correct?).
I have a "cats.db" and "cats.sql" file in the project folder.
This is where I attempt to query from the database:
require_relative 'db_connection'
require 'active_support/inflector'
class SQLObject
def self.columns
cols = DBConnection.execute2(<<-SQL, #table_name)
SELECT
*
FROM
?
SQL
And here is a db_connection.rb file:
require 'sqlite3'
# https://tomafro.net/2010/01/tip-relative-paths-with-file-expand-path
ROOT_FOLDER = File.join(File.dirname(__FILE__), '..')
CATS_SQL_FILE = File.join(ROOT_FOLDER, 'cats.sql')
CATS_DB_FILE = File.join(ROOT_FOLDER, 'cats.db')
class DBConnection
def self.open(db_file_name)
#db = SQLite3::Database.new(db_file_name)
#db.results_as_hash = true
#db.type_translation = true
#db
end
def self.reset
commands = [
"rm '#{CATS_DB_FILE}'",
"cat '#{CATS_SQL_FILE}' | sqlite3 '#{CATS_DB_FILE}'"
]
commands.each { |command| `#{command}` }
DBConnection.open(CATS_DB_FILE)
end
def self.instance
reset if #db.nil?
#db
end
def self.execute(*args)
puts args[0]
instance.execute(*args)
end
def self.execute2(*args)
puts args[0]
instance.execute2(*args)
end
def self.last_insert_row_id
instance.last_insert_row_id
end
private
def initialize(db_file_name)
end
end
Any advice/suggestions welcomed! Thanks!

Related

Can't read JSON file in Ruby on Rails

I am new in ruby on rails and I want to read data from a JSON file from a specified directory, but I constantly get an error in chap3(File name)
Errno::ENOENT in TopController#chap3. No such file or directory # rb_sysopen - links.json.
In the console, I get a message
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
How I can fix that?
Code:
require "json"
class TopController < ApplicationController
def index
#message = "おはようございます!"
end
def chap3
data = File.read('links.json')
datahash = JSON.parse(data)
puts datahash.keys
end
def getName
render plain: "名前は、#{params[:name]}"
end
def database
#members = Member.all
end
end
JSON file:
{ "data": [
{"link1": "http://localhost:3000/chap3/a.html"},
{"link2": "http://localhost:3000/chap3/b.html"},
{"link3": "http://localhost:3000/chap3/c.html"},
{"link4": "http://localhost:3000/chap3/d.html"},
{"link5": "http://localhost:3000/chap3/e.html"},
{"link6": "http://localhost:3000/chap3/f.html"},
{"link7": "http://localhost:3000/chap3/g.html"}]}
I would change these two lines
data = File.read('links.json')
datahash = JSON.parse(data)
in the controller to
datahash = Rails.root.join('app/controllers/links.json').read
Note: I would consider moving this kind of configuration file into the /config folder and creating a simple Ruby class to handle it. Additionally, you might want to consider paths instead of URLs with a host because localhost:3000 might work in the development environment but in production, you will need to return non-localhost URLs anyway.
Rails use the content of file in the controller
#data = File.read("#{Rails.root}/app/controllers/links.json")

Pandas MySQL exception don't shows

I have this code for connect to MySQL through a SSH, inside of a python class:
def executeQuery(self, query_string):
print("connecting to database " + self.sql_main_database)
with SSHTunnelForwarder(
(
self.ssh_host,
self.ssh_port),
ssh_username = self.ssh_user,
ssh_pkey = self.pkey,
remote_bind_address=(self.sql_hostname, self.sql_port)
) as tunnel:
print("performing connection")
conn = pymysql.connect(
host="127.0.0.1",
user=self.sql_username,
password=self.sql_password,
db=self.sql_main_database,
port=tunnel.local_bind_port)
query = query_string
print("Querying")
data = pd.read_sql_query(query, conn)
print("Done!")
conn.close()
return data
The code is working well, but when the query is not well defined, the notebook freezes.
Then, I tried to use a try/catch, and the code ended like this
def executeQuery(self, query_string):
try:
with SSHTunnelForwarder(
(
self.ssh_host,
self.ssh_port
),
ssh_username = self.ssh_user,
ssh_pkey = self.pkey,
remote_bind_address=(self.sql_hostname, self.sql_port)
) as tunnel:
try:
conn = pymysql.connect(
host = "127.0.0.1",
user = self.sql_username,
password = self.sql_password,
db = self.sql_main_database,
port = tunnel.local_bind_port
)
try:
query = query_string
data = pd.read_sql_query(query, conn)
return data
except DatabaseError as e:
Log.debug(self,str(e))
raise DatabaseError
except pymysql.err.InternalError as e:
Log.debug(self, str(e))
raise DataError
except Exception as e:
Log.debug(self, "[Error]Setting up database: \'" + self.sql_main_database + "\'")
raise DataError
The issue is that pd.read_sql_query never stops so the except is never called, the try won't fail, and the process will just continue forever
The timeout workaround is not possible, because the queries don't have defined execution times and some of them can stay in processing during a couple of hours.
I'm not sure how to fix it.
Indeed the problem was not on the connector, just updating the jupyter version was needed.

Fetch rows from Mysql and display it in html using Django

I am fairly new to Django. I want to know how to fetch rows from mysql and get it in views.py and send it to html where it will be displayed.
My views.py:
def fetchDate1(request):
query = request.session.get('query')
date1 = request.session.get('date1');
db = pymysql.connect(host="localhost", # your host
user="root", # username
passwd="=", # password
db="Golden") # name of the database
# Create a Cursor object to execute queries.
cur = db.cursor()
# Select data from table using SQL query.
stmt = "SELECT * FROM golden_response WHERE query LIKE '%s' AND DATE(updated_at) = '%s' " % (
query.replace("'", r"\'"), date1)
cur.execute(stmt)
if cur.rowcount is None:
return None
else:
rows = cur.fetchall()
row_headers = [x[0] for x in cur.description] # this will extract row headers
json_data = []
for result in rows:
json_data.append(dict(zip(row_headers, result)))
return json.dumps(json_data)
I don't know where I am going wrong. Have also saved required configuration in settings.py.
However when i try to run my program :
ProgrammingError: (1146, "Table 'Golden.django_session' doesn't exist")
Please help!!
I dare to hypothesize that you have not made the first migrations.
python manage.py makemigrations
python manage.py migrate
Moreover, you should check the database connection parameters in settings.py like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_user_password',
'HOST': 'db_server',
'PORT': '3306',
}
}

ActiveRecord: Connect to multiple databases one by one

I need to ensure that records on different database servers are present on the backup server.
I'm trying to execute this as a Sinatra project using RSpec:
describe 'BACKUP' do
puts 'BACKUP config'
puts DB_CONFIG[:BACKUP].inspect
p "Key.count #{Key.count}"
DB_CONFIG[:databases].each do |server,config|
p "****************************************************************"
p "Server #{server} "
p " Config #{config.inspect}"
p "Service.count #{Service.new(config).count}"
end
end
Where:
class Key < BillingTables
end
class BillingTables < ActiveRecord::Base
self.abstract_class = true
establish_connection DB_CONFIG[:BACKUP]
end
is connected to one database.
On the other hand I'm connecting simultaneously to another database with this class:
class Service < CoreTables
end
class CoreTables < ActiveRecord::Base
self.abstract_class = true
def initialize(params = {})
establish_connection params
end
end
Output of this script is as follows:
=== Comparison Spec ===
backup config
{"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>true, "database"=>"backup", "pool"=>1, "username"=>"backup", "password"=>"password", "host"=>"xxx.xxx"}
"Key.count 3902"
"****************************************************************"
"Server a1 "
" Config {\"adapter\"=>\"mysql2\", \"encoding\"=>\"utf8\", \"reconnect\"=>true, \"database\"=>\"s1\", \"pool\"=>1, \"username\"=>\"s1\", \"password\"=>\"password\", \"host\"=>\"yyy.yyy\"}"
/Users/password123/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:570:in `retrieve_connection': No connection pool for Service (ActiveRecord::ConnectionNotEstablished)
In the end, I dropped my idea of multipe active_records, and now I'm just performing mysql connections
begin
client = Mysql2::Client.new DB_CONFIG[:databases][server]
query = 'SELECT * FROM test'
result = client.query(client.escape(query),symbolize_keys: true)
rescue Exception => e
p "query error #{e.message}"
next
end

modular sinatra app doesn't log exceptions

I'm creating my first app in Sinatra and I have problems with logging. I read some topics here about logging in Sinatra and thanks to them I can log requests but I cannot see error stacks in my log file - they show only in console.
Here is what I've got so far:
app.rb
require 'rubygems'
require 'bundler'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
require 'sinatra/base'
require 'sinatra/config_file'
require 'sinatra/partial'
class App < Sinatra::Base
configure do
enable :sessions, :logging, :partial_underscores, :dump_errors, :raise_errors
log_file = File.new("#{settings.root}/log/#{settings.environment}.log", 'a+')
log_file.sync = true
use Rack::CommonLogger, log_file
set :root, File.dirname(__FILE__)
set :logger_level, :info
set :logger_log_file, File.join(File.dirname(__FILE__), "/log/#{environment}.log")
helpers do
def logger
#logger ||= begin
#logger = ::Logger.new(self.class.logger_log_file)
#logger.level = ::Logger.const_get((self.class.logger_level || :warn).to_s.upcase)
#logger.datetime_format = "%Y-%m-%d %H:%M:%S"
#logger
end
end
end
end
error do
logger.error env['sinatra.error'].message
redirect to('500.html')
end
end
And when I set
get '/' do
raise 'some test error'
end
Stack trace shows only in console. Any ideas what I'm doing wrong?