Been at this for hours and hours without end debugging why my CREATE TABLE statement failed.
Tested it within a MySQL database and it works just fine. But for some reason all my SELECT's can't get my login table like it doesn't exists(The error says).
Here is my SQL for creating the table.
CREATE TABLE IF NOT EXISTS login(
id SMALLINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_type SMALLINT NOT NULL,
user_data text NOT NULL,
created_on datetime NOT NULL DEFAULT "0000-00-00 00:00:00"
);
I just cannot see where the error is. One other major issue is that after hours while googling i haven't found any solid wiki that provides enough information about the API for me to really use it. So i had to jump around from site to site to get bites of information.
Hope someone can help me sort out the issue with the Query.
I am sry for posting the issue. Already found what i done wrong.
Here is the solution for anyone else who might encounter the same thing.
I simply wrote "AUTO_INCREMENT" as in MySQL where i had to be "AUTOINCREMENT" without the _
Just as a friendly note, the WebSQL Standard isn't recommended: Is it recommended to use the web sql database for storage on the client side
For a different Client-Side Relational Database Solution, try: SequelSphere
It is an HTML5 Relational Database Engine that supports SQL and stores it's data in Local Persistence. It does not use WebSQL databases, but rather is its own SQL engine. As such, it will work in any JavaScript compliant browser. While it currently only supports Local Storage, very soon SequelSphere will support other local persistence engines such as IndexedDB and File API.
For full disclosure: I am related to the company SequelSphere. :)
Related
In the design stage for an app that collects large amounts of data...
Ideally, I want it to be an offline-first app and was looking to Pouchdb/Counchdb - However, the data needs to be kept for years for legal reasons, and my concern is that this is going to consume too much local storage over time.
My thoughts were:
handle sync between pouchdb and couchdb myself, allowing me to purge inactive documents from the local store without impacting the couchdb. This feels messy and probably a lot of work
Build a local store using dexie.js and completely write the sync function. It also looks hard work, but may be less as I'm not trying to mess with a sync function
Search harder :)
Conceptually, I guess I'm looking for a 'DB cache' - holding active json document versions and removing documents that have not been touched for X period. It might be that 'offline' mode is handled separate to the DB cache..
Not sure yet if this is the correct answer..
setup a filter on couchdb to screen out old documents (lets say we have a 'date_modified' field in the doc and we filter out any docs with date_modified older than one month)
have a local routine on the client that deletes documents from the local pouchdb that are older than one month ( actually using the remove() method against the local pouchdb, not updating it with _deleted:true) - from https://pouchdb.com/2015/04/05/filtered-replication.html it appears removed documents don't sync.
docs updated on the Pouchdb will replicate normally
there might be a race condition here for replication, we'll see
i would like to ask for help with my backend flux.
I'm starting to use SQL now, and i have some background in noSQL databases, but i don't know SQL much, so i'm having some trouble finding out how to register my schemas.
I'm using node-mysql, and the way that i can create schemas is calling the method query, like:
myInstance.query( 'CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NULL NULL,
email VARCHAR(100) NULL NULL,
password VARCHAR(100) NULL NULL
)');
The problem with this solution is: this code will run in every server initialization.
So, i would like to know how to check if the schema already exists, and.. is this a good solution?? I was thinking in a bash script that creates all schemas, them i don't need this if statements.
Thanks.
What you are calling a "schema" is really a "table". Hence, create table statement, rather than create schema. This is very important. Perhaps this part of the documentation will help you understand the difference.
There are four very different constructs:
Database Server -- how you connect to one or more databases
Database Instance -- a grouping of objects, typically a unit of backup and storage
Schemas -- a grouping of objects (which may be within a database), typically a unit of permissions
Tables -- where data is stored
Note that different database systems have slightly different variations on these.
Of course, "tables" have schemas, which is why it is easy to get confused.
Generally, the management of the database is handled separately from user applications. That is, the DBA (which might also be the developer) would create the database, manage access, handle backup/recovery, and other things. The application would simply connect to the database and assume that the correct tables and data are there.
That is, under most circumstances, you wouldn't be creating tables in application code. Just use the tables that should already have been created for your database.
You can modify you sql statement to
CREATE TABLE IF NOT EXISTS users (…
That way the code will run on server init, but not do anything and also not fail when the tables are already there. See the corresponding mysql documentation.
Contrary to the answer you got, to have the SQL statements in application code is not that uncommon for backends.
In my application I receive json data in a post request that I store as raw json data in a table. I use postgresql (9.5) and node.js .
In this example, the data is an array of about 10 quiz questions experienced by a user, that looks like this:
[{"QuestionId":1, "score":1, "answerList":["1"], "startTime":"2015-12-14T11:26:54.505Z", "clickNb":1, "endTime":"2015-12-14T11:26:57.226Z"},
{"QuestionId":2, "score":1, "answerList":["3", "2"], "startTime":"2015-12-14T11:27:54.505Z", "clickNb":1, "endTime":"2015-12-14T11:27:57.226Z"}]
I need to store (temporarily or permanently) several indicators computed by aggregating data from this json at quizz level, as I need these indicators to perform other procedures in my database.
As of now I was computing the indicators using javascript functions at the time of handling the post request and inserting the values in my table alongside the raw json data. I'm wondering if it wouldn't be more performant to have the calculation performed by a stored trigger function in my postgresql db (knowing that the sql function would need to retrieve the data from inside the json raw data).
I have read other posts on this topic, but it was asked many years ago and not with node.js, so I thought people might have some new insight on the pros and cons of using sql stored procedures vs server-side javascript functions.
edit: I should probably have mentioned that most of my application's logic already mostly lies in postgresql stored procedures and views.
Generally, I would not use that approach due to the risk of getting the triggers out of sync with the code. In general, the single responsibility principle should be the guide: DB to store data and code to manipulate it. Unless you have a really pressing business need to break this pattern, I'd advise against it.
Do you have a migration that will recreate the triggers if you wipe the DB and start from scratch? Will you or a coworker not realise they are there at a later point when reading the app code and wonder what is going on? If there is a standardised way to manage the triggers where the configuration will be stored as code with the rest of your app, then maybe not a problem. If not, be wary. A small performance gain may well not be worth the potential for lost developer time and shipping bugs.
Currently working somewhere that has gone all-in on SQL functions.. We have over a thousand.. I'd strongly advise against it.
Having logic split between Javascript and SQL is a real pain when debugging issues especially if, like me, you are much more familiar with JS.
The functions are at least all tracked in source control and get updated/created in the DB as part of the deployment process but this means you have 2 places to look at when trying to follow the code.
I fully agree with the other answer, single responsibility principle, DB for storage, server/app for logic.
I want to make a JavaScript PhoneGap app being able to store information, like log in data or for a savegame, but obviously I'm not able to write into files with JavaScript ansd since this is supposed to work offline I am also not able to run a server script that manages it for me.
I'm pretty sure there is a way to do this and I'd be very thankful for your help!
You can use local storage. Local storage only stores strings, so you'll probably want to serialize JSON when saving to local storage. See is a breakdown of local storage size by browser.
var myData = { foo: "bar" };
localStorage.setItem("myData", JSON.stringify(myData));
var retrievedDataString = localStorage.getItem("myData");
var retrievedData = JSON.parse(myDataString);
Just like "dfsq" sayed, have you tried localStorage?
localStorage.userToken = "SomeData";
But remember, localStorage can only storage a max of 5mb of data and all the data is stored as String, for more than this or more flexibility with the type you will need another solution for your problem.
You better use a database. One of the good options are Firebase. https://www.firebase.com/ . Try its tutorial. It gives you offline capability as well. Also its FREE. ( For 50 concurrent connections )
What about using SQlite plugin for phonegap/cordova?
Cordova SQLite plugin
Many users, many solutions. In my mind the best way to do this would be with a WebSQL Database. You may have a look at http://www.html5rocks.com/de/features/storage - i know this site is in german but the graphic displayed there will give you a little overview. You'll see where you can use what kinds of databases.
WebSQL works like a charme and every of my apps with databases uses webSQL Databases. Its not just a good solution, it is the best one to be consistency. Normally you're using a MySQL DB on a WebServer for Logindata etc. if you have already all your data localy stored inside a WebSQL Database, you have also consistency of your
We have an SQLite database lying around, which is curerntly filled from an external batch job. The database is not very complex (essentially two tables in a 1:n relationship and some "catalog tables" holding lookup values).
We now have to add a user-frontend as well as some reporting. At one moment in time only one user is using the frontend, however, this should be possible from everywhere in our network (= wherever access to the SQLite file is possible).
What's the easiest way to create an easy-to-use frontend with as little effort as possible? I thought about using HTML/JS, but haven't found out how to access a local SQLite DB with JS (is this even possible? we could grant the application such access rights of course, however, do browsers even support this?)
If HTML/JS is not an option without a dedicated server, is there any other possiblity to get this done with little effort? We do not want to end up with MS Access... :(
Use the HTA application if not afraid of safety problems. Rename your html file to *.hta, make ODBC connection to your database then:
var Connection = new ActiveXObject ('ADODB.Connection');
Connection.Open (<ODBC-name>);
var Records = new ActiveXObject ('ADODB.Recordset');
Records.Open (Sql, Connection, 0, 2);
See the Properties & Methods for ADO Recordset Object.