I have a problem with my code, I wrote simple flutter app which is create account for persons and save person information in sqlite database, but when I create tables it gave me errors this is my code and error:
my code:
Future<Database> openDb() async {
if (db == null) {
// get path of database
final pathdb = await getDatabasesPath();
final path = join(pathdb, 'roznamcha.db');
db = await openDatabase(path,
// create tables
onCreate: (Database database, int version) async {
await database.execute('''
CREATE TABLE account (
accountNumber INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL,
telphone VARCHAR(10) NOT NULL,
address VARCHAR(100),
issueDate DATETIME,
balance INTEGER,
image VARCHAR(255),
type VARCHAR(64),
)''');
// transaction table
await database.execute('''
CREATE TABLE transaction (
transactionNumber INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
issueDate DATETIME NOT NULL,
credit INTEGER,
debit INTEGER,
accountNum INTEGER,
record BOOLEAN DEFAULT false,
rate INTEGER NOT NULL
)''');
}, version: 1);
}
return db;
}
error :
Exception has occurred. SqfliteDatabaseException
(DatabaseException(near ")": syntax error (code 1 SQLITE_ERROR): ,
while compiling: CREATE TABLE account (
accountNumber INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL,
telphone VARCHAR(10) NOT NULL,
address VARCHAR(100),
issueDate DATETIME,
balance INTEGER,
image VARCHAR(255),
type VARCHAR(64),
)) sql ' CREATE TABLE account (
accountNumber INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL,
telphone VARCHAR(10) NOT NULL,
address VARCHAR(100),
issueDate DATETIME,
balance INTEGER,
image VARCHAR(255),
type VARCHAR(64),
)' args []})
Related
this is my first hands-on experience with Sql and I'm trying to construct One-To-Many relationship, here is the SQL code:
create table car (
id SERIAL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19, 2) NOT NULL
);
create table person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
email VARCHAR(100),
date_of_birth DATE NOT NULL,
country_of_birth VARCHAR(50) NOT NULL,
cars_owning INT[] REFERENCES car(id)
);
and I get this error:
Key columns "cars_owning" and "id" are of incompatible types: integer[] and integer.
Here is the model a person can have many cars but each car have only one owner
typically (as a good general rule of thumb) the many refers to the one. not the other way around as you have tried.
So, alter the schema as:
create table car (
id SERIAL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19, 2) NOT NULL,
owner_id BIGINT FOREIGN KEY REFERENCES person(id)
);
create table person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
email VARCHAR(100),
date_of_birth DATE NOT NULL,
country_of_birth VARCHAR(50) NOT NULL
);
If you need to get the list of cars owned by a person, use this select statement
SELECT person.id, ARRAY_AGG(car.id) owns_car_ids
FROM person
LEFT JOIN car ON person.id = car.owner_id
GROUP BY 1
The ArtistID column in Piece table refers to the ArtistID in the Artist table. Likewise, the LocationID column in Piece refers to LocationID in the GeographicLocation table. However, both foreign key references throw a "not the same data type as referencing column" error. What am I doing wrong?
CREATE TABLE dbo.Artist
(
ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Sex CHAR(1) NOT NULL CHECK(Sex = 'F' OR Sex = 'M')
)
CREATE TABLE dbo.Piece
(
PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Artist(ArtistID),
LocationID SMALLINT NOT NULL
FOREIGN KEY REFERENCES GeographicLocation(LocationID),
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Appraiser(AppraiserID)
)
CREATE TABLE dbo.GeographicLocation
(
LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL
)
I think you missed a table here, which is AppraiserID column in Piece table refers to the AppraiserID in the Appraisertable
So, when I execute your SQL I am getting below error:
Foreign key 'FK__Piece__Appraiser__322C6448' references invalid table 'Appraiser'.
First Create a table called Appraiser, then create Foreign key references for those column.
Below SQL I am able to create all tables and Foreign key references:
CREATE TABLE dbo.Artist
(ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Gender CHAR(1) NOT NULL CHECK(Gender = 'F' OR Gender = 'M'))
CREATE TABLE dbo.GeographicLocation
(LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL)
CREATE TABLE dbo.Appraiser
(AppraiserID SMALLINT PRIMARY KEY IDENTITY(1, 5),
AppraisedValue MONEY NOT NULL,
AppraisedName VARCHAR(100) NULL)
CREATE TABLE dbo.Piece
(PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL,
LocationID SMALLINT NOT NULL,
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL)
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_ArtistID FOREIGN KEY(ArtistID)
REFERENCES dbo.Artist (ArtistID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_ArtistID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_AppraiserID FOREIGN KEY(AppraiserID)
REFERENCES dbo.Appraiser (AppraiserID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_AppraiserID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_LocationID FOREIGN KEY(LocationID)
REFERENCES dbo.GeographicLocation (LocationID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_LocationID
GO
The problem I have been provided is:
Write the DDL to construct the Facebook User and Post tables in SQL server. You need to populate the user table with the user’s information provided as in Figure 1 and additional attributes may be needed for the design. Use the ID in the excel file to identify the other Facebook user. For example ID 612831408762037 is David Cunliffe. Import the excel sheets to SQL server and then write effective SQL to merge the two tables into the Post table.
The code I have written so far is:
IF EXISTS(
SELECT *
FROM sys.tables
WHERE name = N'FacebookUser'
)
DROP TABLE FacebookUser;
IF EXISTS(
SELECT *
FROM sys.tables
WHERE name = N'Post'
)
DROP TABLE Post;
CREATE TABLE FacebookUser
(UserID varchar(15) primary key not null,
UserName varchar(30) not null,
UserDescript varchar(500),
JoinDate date not null,
HomeTown varchar(30),
Affiliation varchar(30),
Country varchar(30),
CurrentOffice varchar(100),
Gender varchar(6) not null,
Email varchar(30),
OtherAccounts varchar(100),
Website varchar(500),
);
CREATE TABLE Post
(id char(28) not null,
message varchar(8000) not null,
type varchar(15) not null,
created_time datetime not null,
updated_time datetime not null,
shares.count int not null,
count of likes.name int not null,
count of comments.message int not null,
Link varchar(50) not null,
name varchar(50) not null,
Description varchar(200) not null,
);
INSERT INTO FacebookUser
VALUES('612831408762037', 'Some Name', 'Some very long text to insert in my row just for test cases',
'02/18/2009', 'New Lynn/Auckland', 'New Zealand Labour Party', 'New Zealand', 'Office: Member of Parliament for New Lynn Party: New Zealand Labour Party', 'Male',
'Mailadress#example.com', 'Some Name (Other Service)', 'www.example.com');
INSERT INTO FacebookUser
VALUES('1119736203', 'Another Name', 'Some other example text, with some extend ', '02/20/2008', NULL,
NULL, 'New Zealand', 'Office: Prime Minister Party: NZ National Party', 'Male', 'a.someone#example.com', NULL,
'http://example.com');
I have imported the data, but am unsure how merge the data into the second table, and I get errors on these lines of code:
shares.count int not null,
Link varchar(50) not null,
name varchar(50) not null,
Description varchar(200) not null,
I'm completely lost from here. Is anybody able to help?
You will have to use square brackets for field names which are keywords or containing spaces or invalid characters
[shares.count] int not null,
[count of likes.name] int not null,
[count of comments.message] int not null,
In addition you will have to make sure that the fields are big enough to keep the values you want to store, other ways you will get errors like String or binary data would be truncated.
I'm trying to create an Oracle 11g table, but for some reason it says that parenthesis is missing.
CREATE TABLE logentry (
id CHAR(36) NOT NULL,
"user" VARCHAR2(8 CHAR),
timestamp DATE NOT NULL,
description VARCHAR2(32 CHAR) NOT NULL,
details TEXT(4096),
attn SMALLINT,
readonly SMALLINT,
hasdata SMALLINT,
task_id CHAR(36),
"_type" VARCHAR2(32 CHAR) NOT NULL,
PRIMARY KEY (id)
)
Oracle has no TEXT data type. If you need to store amount of character data greater than 4000, you could use CLOB(character large object) data type, which will allows you to store up to 4GB of data:
CREATE TABLE logentry (
id CHAR(36) NOT NULL,
"user" VARCHAR2(8 CHAR),
timestamp DATE NOT NULL,
description VARCHAR2(32 CHAR) NOT NULL,
details CLOB,
attn SMALLINT,
readonly SMALLINT,
hasdata SMALLINT,
task_id CHAR(36),
"_type" VARCHAR2(32 CHAR) NOT NULL,
PRIMARY KEY (id)
)
Just trying to create a new database table on plugin activation.
For the love of life I cannot figure out why this will not work.
function super_simple_photo_activate() {
global $wpdb;
$table_name = $wpdb->prefix."super_simple_photo_options";
if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) {
$sql = 'CREATE TABLE '.$table_name.'(
thumbs_max VARCHAR(3),
image_max VARCHAR(4),
image_quality VARCHAR(3),
PRIMARY KEY (id))';
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("super_simple_photo_db_version", "1.0");
}
}
register_activation_hook(__FILE__, 'super_simple_photo_activate');
I've spent at least 5 hours tinkering with this but with no luck, no error either on activation.
What did the trick - id INTEGER NOT NULL - thanks t.thielemans
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL,
thumbs_max VARCHAR(3),
image_max VARCHAR(4),
image_quality VARCHAR(3),
PRIMARY KEY (id))';
Try this code
register_activation_hook ( __FILE__, 'on_activate' );
function on_activate() {
global $wpdb;
$create_table_query = "
CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}table1` (
`id` bigint(20) unsigned NOT NULL default '0',
`name` text NOT NULL,
`address` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_table_query );
}
Your create table syntax is wrong, should be:
$sql = 'CREAT TABLE '.$table_name.'(
-----
$sql = 'CREATE TABLE '.$table_name.'(
Edit: Define your primary key
$sql = 'CREATE TABLE '.$table_name.'(
id INTEGER NOT NULL,
thumbs_max VARCHAR(3),
image_max VARCHAR(4),
image_quality VARCHAR(3),
PRIMARY KEY (id))';
Bit of extra info on SQL from W3schools: http://www.w3schools.com/sql/sql_primarykey.asp