I've downloaded a project from internet, that is supposed to let me draw some polygons, points and so on on the map, then save it on the PostgreSQL database. You can also upload KML files to show already drawn points,polygons, etc - that doesn't work as well.
The project is using PostGis + GeoServer.
The problem is, I don't know how to enable database in it to save the coordinates.
So far I did:
1)Install PostgreSQL
2)Install PostGis
3)Install GeoServer
4)Install WAMP
5)Create database called 'parking'
6) In the 'parking' I've run SQL queries like this :
-- After creating database
CREATE EXTENSION postgis;
-- CREATE SEQUENCE FOR TABLE parking_spaces
CREATE SEQUENCE public.sq_parking_spaces
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
-- TABLE parking_spaces
CREATE TABLE public.parking_spaces
(
id integer NOT NULL DEFAULT nextval('sq_parking_spaces'::regclass),
name character varying(80),
paid boolean,
spaces integer,
geometry geometry(Polygon,3857),
CONSTRAINT parking_spaces_pkey PRIMARY KEY (id)
)
-- CREATE SEQUENCE FOR TABLE parking_meters
CREATE SEQUENCE public.sq_parking_meters
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
-- TABLE parking_meter
CREATE TABLE public.parking_meters
(
id integer NOT NULL DEFAULT nextval('sq_parking_meters'::regclass),
name character varying(80),
geometry geometry(Point,3857),
CONSTRAINT parking_meters_pkey PRIMARY KEY (id)
)
What should be my next goal? How do I check the tables, using PgAdmin?
EDIT:
The question is how to properly connect PostgreSQL database to GeoServer? And how to give GeoServer full write access to layers?
In continuation to the links shared above, here are the generic steps to ensure that the configuration works well:
Make sure that WAMP is installed successfully and is working.
PostgreSQL is installed successfully and that you're able to run the queries using an admin interface.
Create a new user (who doesn't necessarily have to be a superuser):
https://www.postgresql.org/docs/9.1/static/app-createuser.html
GRANTpermissions for SELECT, INSERT, UPDATE and DELETE for this new user on your database: https://www.postgresql.org/docs/9.0/static/sql-grant.html
In the context of this particular problem, add Service Level Security for the application: http://docs.geoserver.org/stable/en/user/security/service.html
Ensure that you have the write access for the Layers. In loose terms, there will have to be one layer in Geoservices per table in the DB: http://docs.geoserver.org/stable/en/user/security/layer.html
Finally, when attempting to invoke WFS calls to the Services, the parameters in your jQuery must be set as described at the following link: https://gis.stackexchange.com/questions/21251/how-to-initialize-a-wfs-layer
Hope that helps.
From your commands above it doesn't appear as if you have added the geometry column to the geometry_columns table - use the AddGeometryColumn statement to do this.
The next thing to try is to work through the GeoServer tutorial on PostGIS.
Related
Error
SQL query:
--
-- Database: `work`
--
-- --------------------------------------------------------
--
-- Table structure for table `administrators`
--
CREATE TABLE IF NOT EXISTS `administrators` (
`user_id` varchar( 30 ) NOT NULL ,
`password` varchar( 30 ) NOT NULL ) ENGINE = InnoDB DEFAULT CHARSET = latin1;
MySQL said:
#1046 - No database selected
need some help here.
You need to tell MySQL which database to use:
USE database_name;
before you create a table.
In case the database does not exist, you need to create it as:
CREATE DATABASE database_name;
followed by:
USE database_name;
You can also tell MySQL what database to use (if you have it created already):
mysql -u example_user -p --database=example < ./example.sql
I faced the same error when I tried to import a database created from before. Here is what I did to fix this issue:
1- Create new database
2- Use it by use command
3- Try again
This works for me.
If you're trying to do this via the command line...
If you're trying to run the CREATE TABLE statement from the command line interface, you need to specify the database you're working in before executing the query:
USE your_database;
Here's the documentation.
If you're trying to do this via MySQL Workbench...
...you need to select the appropriate database/catalog in the drop down menu found above the :Object Browser: tab. You can specify the default schema/database/catalog for the connection - click the "Manage Connections" options under the SQL Development heading of the Workbench splash screen.
Addendum
This all assumes there's a database you want to create the table inside of - if not, you need to create the database before anything else:
CREATE DATABASE your_database;
If you are doing this through phpMyAdmin:
I'm assuming you already Created a new MySQL Database on Live Site (by live site I mean the company your hosting with (in my case Bluehost)).
Go to phpMyAdmin on live site - log in to the database you just created.
Now IMPORTANT! Before clicking the "import" option on the top bar, select your database on the left side of the page (grey bar, on the top has PHP Myadmin written, below it two options:information_schema and name of database you just logged into.
once you click the database you just created/logged into it will show you that database and then click the import option.
That did the trick for me. Really hope that helps
For MySQL Workbench
Select database from Schemas tab by right mouse clicking.
Set database as Default Schema
Edit your SQL file using Notepad or Notepad++
add the following 2 line:
CREATE DATABASE NAME;
USE NAME;
Assuming you are using the command line:
1. Find Database
show databases;
2. Select a database from the list
e.g. USE classicmodels; and you should be off to the races! (Obviously, you'll have to use the correctly named database in your list.
Why is this error occurring?
Mysql requires you to select the particular database you are working on. I presume it is a design decision they made: it avoids a lot of potential problems: e.g. it is entirely possible, for you to use the same table names across multiple databases e.g. a users table. In order to avoid these types of issues, they probably thought: "let's make users select the database they want".
If importing a database, you need to create one first with the same name, then select it and then IMPORT the existing database to it.
Hope it works for you!
be careful about blank passwords
mysqldump [options] -p '' --databases database_name
will ask for a password and complain with mysqldump: Got error: 1046: "No database selected" when selecting the database
the problem is that the -p option requires that there be no space between -p and the password.
mysqldump [options] -p'' --databases database_name
solved the problem (quotes are not needed anymore).
Check you have created the database first which you want.
If you have not created the dataBase you have to fire this query:
CREATE DATABASE data_base_name
If you have already created the database then you can simply fire this query and you will be able to create table on your database:
CREATE TABLE `data_base_name`.`table_name` (
_id int not null,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (_id)
);
Solution with an Example
Error 1046 occurs when we miss to connect our table with a database. In this case, we don't have any database and that’s why at first we will create a new database and then will instruct to use that database for the created table.
# At first you have to create Database
CREATE DATABASE student_sql;
# Next, specify the database to use
USE student_sql;
# Demo: create a table
CREATE TABLE student_table(
student_id INT PRIMARY KEY,
name VARCHAR(20),
major VARCHAR(20)
);
# Describe the table
describe student_table;
quoting ivan n :
"If importing a database, you need to create one first with the same name, then select it and then IMPORT the existing database to it.
Hope it works for you!"
These are the steps:
Create a Database, for instance my_db1, utf8_general_ci.
Then click to go inside this database.
Then click "import", and select the database: my_db1.sql
That should be all.
first select database : USE db_name
then creat table:CREATE TABLE tb_name
(
id int,
name varchar(255),
salary int,
city varchar(255)
);
this for mysql 5.5 version syntax
I'm late i think :] soory,
If you are here like me searching for the solution when this error occurs with mysqldump instead of mysql, try this solution that i found on a german website out there by chance, so i wanted to share with homeless people who got headaches like me.
So the problem occurs because the lack -databases parameter before the database name
So your command must look like this:
mysqldump -pdbpass -udbuser --databases dbname
Another cause of the problem in my case was that i'm developping on local and the root user doesn't have a password, so in this case you must use --password= instead of -pdbpass, so my final command was:
mysqldump -udbuser --password= --databases dbname
Link to the complete thread (in German) : https://marius.bloggt-in-braunschweig.de/2016/04/29/solution-mysqldump-no-database-selected-when-selecting-the-database/
In Amazon RDS, merely writing use my-favorite-database does not work if that database's name includes dashes. Furthermore, none of the following work, either:
use "my-favorite-database"
use `my-favorite-database`
use 'my-favorite-database'
Just click the "Change Database" button, select the desired database, and voilà.
Although this is a pretty old thread, I just found something out. I created a new database, then added a user, and finally went to use phpMyAdmin to upload the .sql file. total failure. The system doesn't recognize which DB I'm aiming at...
When I start fresh WITHOUT first attaching a new user, and then perform the same phpMyAdmin import, it works fine.
Just wanted to add: If you create a database in mySQL on a live site, then go into PHPMyAdmin and the database isn't showing up - logout of cPanel then log back in, open PHPMyAdmin, and it should be there now.
For an added element of safety, when working with multiple DBs in the same script you can specify the DB in the query, e.g. "create table my_awesome_db.really_cool_table...".
jst create a new DB in mysql.Select that new DB.(if you r using mysql phpmyadmin now on the top it'l be like 'Server:...* >> Database ).Now go to import tab select file.Import!
I am creating a system to generate unique keys. It works for now. But, I haven't tested it with many users. Users may click a button and then get his unique number, as simple as that.
But, How to prevent multiple users getting the same unique keys,if they press the button exactly in the same time (even in ms scale)? The button is on client side, so I must do something in the back end.
This is the unique key looks like:
19/XXXXXX-ABC/XYZ
The XXXXXX is auto increment number from 000001 to 999999. I have this code but didn't know if it's reliable enough to handle my issue.
$autoinc = $this->MPenomoran->get_surat($f_nomor)->jumlah_no+1; //count data in table and added 1
$no_1 = date('y')+2;
$no_2 = str_pad($autoinc, 6, '0', STR_PAD_LEFT);
$no_3 = "-ABC/XYZ";
$nomor = $no_1."/".$no_2.$no_3;
$returned_nomor = $nomor;
$success = array ('nomor' => $returned_nomor); //sent unique keys to user's view
It seems like you don't want to come out and tell us what the platform is for this, or what the limitations to that platform are.
The first thing that jumps out is that your format is limited by year, to 999999 total unique keys. Very odd, but presumably you understand that limit, and would need to put in some code to deal with hitting the maximum number.
Approaches
REDIS based
This would be very simple with a REDIS server using the INCR. Since INCR is atomic, you essentially have a solution just by creating a key named for your year + 2, should it not exist, and using INCR on it from there on out.
You would need to utilize some php redis client, and there are a variety of them with strengths and weaknesses to each that I'm not going to go into.
Redis is also great for caching, so if at all possible that is the first thing I would look into.
MySQL Based
There are a few different solutions using mysql. They are involved, so I'll just outline them because I don't want to spend time writing a novel.
Note: You will need to translate these into the appropriate PHP code (mysqli or PDO) where as noted, parameters are passed, transactions started etc.
MySQL - create your own sequence generator
Create a table named "Sequence" with this basic structure:
name varchar(2) PK
nextval unsigned int default 1
engine=InnoDB
The underlying query would be something like this:
BEGIN_TRANS;
SELECT nextval FROM Sequence WHERE name = '$no_1' FOR UPDATE;
UPDATE Sequence SET nextval = nextval + 1;
END_TRANS;
This code emulates a serialized Oracle style sequence. It is safe from a concurrency standpoint, because MySQL will lock the row briefly, then increment it upon completion.
MySQL - Autoincrement on multi-value PK
This comes with some caveats.
It is generally incompatible with replication.
The underlying table must be myisam
name varchar(2) PK
lastval unsigned int AUTO_INCREMENT PK
engine=MyISAM
Your underlying query would be:
INSERT INTO Sequence (name) VALUES ('$no_1')
This depends on mysql supporting a multi-column key where the 2nd column is an AUTO_INCREMENT. It's behavior is such that it acts like a sequence for each unique name.
You would then use the relevant api's built-in approach to getting the mysql LAST_INSERT_ID(). For example with PDO
Other alternatives
You could also use semaphores, files with locking, and all sorts of other ideas to create a sequence generator that would work well in a monolithic (one server for everything) environment. MySQL and Redis would serve a cluster, so those are more robust options from that standpoint.
The important thing is that whatever you do, you test it out using a load tester like siege or Boom to generate multiple requests at your web level.
My question is I have a database named Autodb, this has a table named Bluetooth in phpmyadmin in which I have to create a column named LAP. In this column user should enter any value between 0X9E8B00-0X9E8B3F, is there a way to do it in phpmyadmin and is there a query in SQL to set the range of values to be entered from?
Im not 100% sure what you're asking but this might help:
If you want to set each value the user can enter you can use an ENUM data type.
Have a look at ENUM here
Here is an example of how to use it.
CREATE TABLE Bluetooth
(
ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
BlahBlah VARCHAR(50) NOT NULL,
LAP ENUM('0X9E8B00', '0X9E8B3F', '0X9E8B3F') NOT NULL
)
If the range is between 2 numbers you can use a constraint and CHECK
ALTER TABLE myTableName
ADD CONSTRAINT myTableName_myColumnName_valZeroToOneHundred
CHECK (myColumnName BETWEEN 0 AND 100)
ALTER TABLE Table
ADD CONSTRAINT CK_Table_Column_Range CHECK (
Column >= 0 AND Column <= 100 --Inclusive
)
Check this question out.
If you want only insert data between range:
INSERT INTO Bluetooth (ID, BLAHBLAH, LAP)
SELECT (1, 'BlahBlah', 0x9e8f21) WHERE
IF (0x9e8f21 >= 0x9e8b00 and 0x9e8f21 <= 0x9e8b3f, 1, 0) = 1
phpMyAdmin is a tool for administrators to manage a MySQL database, not an end-user front end, so this is a bit beyond the scope of what phpMyAdmin can handle.
Basically you have three choices when it comes to verifying data on insert:
Application level
In your front end, which appears to be PHP based on the tag you've used, check the value before passing it to MySQL at all. You should be able to do standard hex comparisons on the values and print an error message if it's outside the bounds you set.
MySQL trigger
MySQL allows you to set up a trigger which can check the data before the INSERT occurs. A trigger is a command or test that runs before (or after) an event such as INSERT, UPDATE, or DELETE — you'd probably want the same trigger action to occur after an INSERT or DELETE. You can return an error message to the client if the value is outside your bounds. MySQL should be able to compare hex values.
MySQL stored procedure
Instead of calling directly the INSERT command, you could call a stored procedure. This has the advantage of being written once for all the INSERTs you'd perform regardless of what front end you use; they'd all use the stored procedure. If you change the bounds, you'll do so in the stored procedure instead of the application code, which is good if you have multiple deployments or applications accessing the data. Again, MySQL should be able to handle the hex values.
Which one you select depends on your deployment and experience level with each, but in all these cases you're able to achieve the checks you're trying for.
I'm freaking out. I work at a company that (incredibly stupidly) doesn't back up their MySQL database. I had to change one of the columns in a table "items" in order to raise the max character count for an item's description. I used the following command:
ALTER TABLE items CHANGE description description varchar(5000) NOT NULL;
But, after I entered this command, I got this error:
Error on rename of './company/#sql-b30_400ad' to './company/items' (errno: 150 - Foreign key constraint is incorrectly formed)
Now, the table is completely gone. Is there a way to access the "#sql-b30_400ad" table to recover it? I know there was a key removed from this table with the following method:
SET foreign_key_checks = 0;
ALTER TABLE items DROP KEY foreign_key_name;
SET foreign_key_checks = 1;
Is there any way to undo what happened, or to recover the temporary items table that was created for the ALTER command?
EDIT: The table uses the InnoDB engine
EDIT AGAIN: So I found out that since we use Amazon Web Service for our MySQL Server, AWS automatically backs it up daily, the last backup being conveniently 15 minutes before my error. My pants are brown and yellow now, but all is good and I extend my thanks to everyone who answered!
You can read documentation about the command :
check
repair
and the tool
mysqlcheck
myisamchk
...
My advice : But first of all if data are very important, for safety reason you may have to stop any read/write process on the disk, and may be stop the disk.
In future : Always make your own backup before any modification, even for these repair/check commands
mysqldump
mysqlhotcopy
check this
http://www.thegeekstuff.com/2014/04/recover-innodb-mysql/
Is it production DB or Development DB, I would not worry if it is development,
Check out unDROP tool for InnoDB. It allows to read records directly from InnoDB tablespace.
#sql-b30_400ad is a file with the new structure. So, split it with stream_parser and then extract records with c_parser. See https://twindb.com/undrop-tool-for-innodb/ , your case fits pretty well into the description.
This seems to be a simple problem, but after a while of searching I can't figure out the answer.
I currently have a MySQL table in my local database used by a webapp, and them same table on a database in a remote server. Right now, I'm using the CREATE TABLE IF NOT EXISTS command through PHP to create the table on the databases:
CREATE TABLE IF NOT EXISTS users (
`id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(18) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
However, let's say I make a modification to the local database, adding a collumn, for example. It would be really annoying to have to go and change the remote database every time I change the local one. Is there an easier way to run code to create a table if it doesn't exist, and if it does exist, make sure it's structure matches that of the create table structure?
Here's an example, to make what I'm trying to convey a little clearer. Let's say on the local database I have a users table, and I decide that in my webapp I want to have another collumn, password. So I go to the local database and add a password collumn. Is there PHP/MySQL code I can run to check if the users table exists, and if it does, make sure it has a password collumn, and if not, add it?
What you are actually looking for are Migrations, e.g. you are looking for a Schema Management Tool that lets you manage your Database structure in versioned code diffs.
For instance, for your described scenario you would first create a script to create the table, e.g. 001_create_user_table.sql. Then you'd use the schema manager to connect and deploy these changes to your databases.
When you want to change or add something, you just write another script, for instance, 002_Add_Password_Column_To_User_Table.sql. Fill in just the code to do that change. Then run the schema manager again.
Typically, you tell the Schema Manager to go through all existing migrations files. On each run, the Schema manager will update a changelog table in the database, so when you run it, it will know which of your scripts it should apply.
The good thing is, you can add these migrations to your regular VCS, so you will always know which database schema you had at which version of your application. And you will have a proper changelog for them.
To directly answer your question you can create temporary procedures to detect field existence like using a query like this:
SHOW COLUMNS FROM table_name LIKE 'column_name';
However in the real world, database changes are general rolled into three scripts. A create script and two deltas one up and one down. Then the database is versioned so that you know at what state the database is in at any given time.
To specifically check for a password column you can use DESCRIBE:
$colExists = false;
$res = mysql_query('DESCRIBE `users`');
while ($row = mysql_fetch_assoc($res)) {
if ($row['Field'] == 'password') {
$colExists = true;
break;
}
}
if (!$colExists) {
// create column
}
However, you should check into replication or some other automated tool to see if they would be a better solution for you.
Follow these steps (you can easily implement this in PHP, I assumed that the name of the table is Foo)
1.) Run the following code:
desc Foo
2.) Based on the result of the first step you can make your create table command (and you should)
3.) Store your data from the existing table which will be replaced in a variable (Optional, you only need this if you can potentially use data from the old table)
4.) Modify the extracted rows from step 3.) so they will be compatible with your new definition (Optional, you only need this if you can potentially use data from the old table)
5.) Get the rows from your new Foo table
6.) Merge the results got in steps 4.) an 5.) (Optional, you only need this if you can potentially use data from the old table)
7.) Run a drop table for the old table
8.) Generate a replace into command to insert all your rows into the newly created Foo table (you can read more about this here)
After these steps, as a result, you will have the new version of the table. If your tables are too large, you can do a CREATE TABLE IF NOT EXISTS command and if that was not successful, run the alter command.
Also, you can make a library to do these steps and will use that in the future instead of solving the same problem several times.
EDIT:
You can connect the database using this function: mysql-connect (documentation here)
You can run a query using this function: mysql-query (documentation here)
Based on the first step you will get the field names (let's assume you store it in a variable called $bar) and you can use your result to generate your select command (connecting to the database where you have important data. It may be both):
$field_list = "1";
foreach ($bar as $key => $value)
$field_list.= ",".$bar[$key];
mysql_connect(/*connection data*/);
mysql_query("select ".$field_list." from Foo");
You can use your new resource to build up an insert command to insert all your important data after deletion recreation (about resources read more here, about how you can generate your insert you can read here, but I suggest that you should use replace into instead of insert which works like the insert, except that it replaces the row if it already exists, it's better here than an insert, read more here)
So, use mysql_connect and mysql_query, and the resource returned by the mysql_query function can be used for replace into later (I've linked now the URL's for everything you need, so I'm pretty sure you'll solve the problem.), apologies for being not specific enough before.