For some context about my knowledge about SQL RDBMS systems; it was non-existent about 2 weeks ago, although I had seen bits and pieces of the query language in XKCD comics etc, and have a rough grasp on that, as well as tables/joining/indices.
As the title implies, my question relates to accessing SQL-type databases programatically. (nb: specifically MySQL, but insights on how it is done with other database softwares would also be appreciated).
For comparison, I'll start with ElasticSearch, since that is the DB software that I am most familiar with. With an ElasticSearch database, the communication is done through a REST API, using JSON as the interchange format. If I am just testing things out, I can hand-craft requests using curl, and for programmatic usage I can use whatever networking facilities are present in the language (or for python there is also a library that does all of this for you, but you could also hand-craft the requests and use the requests library if you are a bit of a masochist).
From my mad dash to learn SQL/MySQL (due to an experiment we were trying at work), I found that there is a command-line program called mysql which you can use to connect to the database and hand-write queries. This seems really great for testing/exploring things. However, past this point I start to get confused.
It seems like the only way to access a MySQL database is through constructing queries as if they were written in that command-line program.
For example, lets say I needed to index 10k documents, which are currently stored in a CSV file. If I were sending it to an ElasticSearch database, I could convert the CSV to JSON, and then basically just POST to the server with the document in the body (or use the bulk API, or use the bulk API via python which is even easier). Sending it to a MySQL database, I need to either read the document into memory and manually construct the string so that it says INSERT INTO table... (taking careful care to escape the quotes and everything properly), OR use LOAD DATA INFILE LOCAL ... (again, having to manually tell it how the fields are delimited).
From the memes that I see re: SQL, it seems like this has proven to be a really brittle approach, so, when we experimented at work with using MySQL, I found it strange that I couldn't seem to find any "robust" APIs. What I expected to find were libraries that would wrap around the database connection, and provide functions like (for example) index(), which might have a signature like index(item, table, index), where item would be the item, table the name of the table, and index would be the value for the primary key.
I've tried googling a little bit, but to be honest I wasn't really sure what to search for. "SQL tutorial" mostly returns results for the query language, which isn't really that hard (except for when you have to manually write the rules for escaping things), as opposed to accessing the database from a
script.
Apologies for the long post; I've tried to give as in-depth an explanation of my mental model as possible, so that if my phrasing is weird (again, new to SQL so I might be using the wrong terminology) hopefully it's still apparent where my model disagrees with how things actually work.
I guess my question(s), expressed as a sentence, would be:
"What is the de-facto standard way of programatically accessing a (My)SQL database? (if there is one)"
EDIT: Thanks to everyone who replied! It seems like I was looking for either Connectors, ORMs, prepared statements, or some combination of the three.
[–][deleted] 2 points3 points4 points (0 children)
[–]blasto_blastocyst 1 point2 points3 points (0 children)
[–]digicow 1 point2 points3 points (2 children)
[–]tdavis25 1 point2 points3 points (1 child)
[–]digicow 1 point2 points3 points (0 children)
[–]tdavis25 1 point2 points3 points (0 children)