I am creating a flask backend that saves details of a DND style character on an SQL server. This is the application script for the input of the character details.
#app.route("/add_char", methods=["GET", "POST"])
#login_required
def add_char():
if request.method == "POST":
cha_id = db.execute("""INSERT INTO
character_basics (
char_id, user_id, name, race, cha_class, level, experience, alignment_lc, alignment_ge,
height, weight, handedness, eye_colour, hair_colour, sex, age, status,
max_hp, current_hp, strength, dexterity, constitution, intelligence,
wisdom, charisma, fortune
) VALUES (
:char_id, :user_id, :name, :race, :cha_class, :level, :experience, :alignment_lc, :alignment_ge,
:height, :weight, :handedness, :eye_colour, :hair_colour, :sex, :age, :status,
:max_hp, :current_hp, :strength, :dexterity, :constitution, :intelligence,
:wisdom, :charisma, :fortune
)
""",
user_id = session["user_id"],
name = request.form.get("name"),
race = request.form.get("race"),
cha_class = request.form.get("cha_class"),
level = request.form.get("level"),
experience = request.form.get("experience"),
alignment_lc = request.form.get("alignment_lc"),
alignment_ge = request.form.get("alignment_ge"),
height = request.form.get("height"),
weight = request.form.get("weight"),
handedness = request.form.get("handedness"),
eye_colour = request.form.get("eye_colour"),
hair_colour = request.form.get("hair_colour"),
sex = request.form.get("sex"),
age = request.form.get("age"),
status = request.form.get("status"),
max_hp = request.form.get("max_hp"),
current_hp = request.form.get("current_hp"),
strength = request.form.get("strength"),
dexterity = request.form.get("dexterity"),
constitution = request.form.get("constitution"),
intelligence = request.form.get("intelligence"),
wisdom = request.form.get("wisdom"),
charisma = request.form.get("charisma"),
fortune = request.form.get("fortune")
)
db.execute("""INSERT into
char_academic (
cha_id, anatomy, anatomy_basic, animal_lore, animal_lore_spec,
appraisal, appraisal_spec, arcana, arcana_spec, botany_forestry,
botany_spec, engineering, engineering_spec, history, history_spec,
mathematics, mathematics_spec, religion, religion_spec
) VALUES (
:cha_id, :anatomy, :anatomy_spec, :animal_lore, :animal_lore_spec,
:appraisal, :appraisal_spec, :arcana, :arcana_spec, :botany_foresty,
:botany_spec, :engineering, :engineering_spec, :history, :history_spec,
:mathematics, :mathematics_spec, :religion, :religion_spec
)
""",
anatomy = request.form.get("anatomy"),
anatomy_spec = request.form.get("anatomy_spec"),
animal_lore = request.form.get("animal_lore"),
animal_lore_spec = request.form.get("animal_lore_spec"),
appraisal = request.form.get("appraisal"),
appraisal_spec = request.form.get("appraisal_spec"),
arcana = request.form.get("arcana"),
arcana_spec = request.form.get("arcana_spec"),
botany_forestry = request.form.get("botany_forestry"),
botany_spec = request.form.get("botany_spec"),
engineering = request.form.get("engineering"),
engineering_spec = request.form.get("engineering_spec"),
history = request.form.get("history"),
histroy_spec = request.form.get("history_spec"),
mathematics = request.form.get("mathematics"),
mathematics_spec = request.form.get("mathematics_spec"),
religion = request.form.get("religion"),
religion_spec = request.form.get("religion_spec")
)
db.execute("""INSERT into
char_profession (
char_id, agriculture, agriculture_spec, blacksmithing,
blacksmithing_spec, commerce, commerce_spec, jeweller,
jeweller_spec, leather_working, leather_spec, masonry,
masonry_spec, metal_working, metal_working_spec, sailing,
sailing_spec, tailoring, tailoring_spec, woodworking,
woodworking_spec
) VALUES (
:char_id, :agriculture, :agriculture_spec, :blacksmithing,
:blacksmithing_spec, :commerce, :commerce_spec, :jeweller,
:jeweller_spec, :leather_working, :leather_spec, :masonry,
:masonry_spec, :metal_working, :metal_working_spec, :sailing,
:sailing_spec, :tailoring, :tailoring_spec, :woodworking,
:woodworking_spec
)
""",
agriculture = request.form.get("agriculture"),
agriculture_spec = request.form.get("agriculture_spec"),
blacksmithing = request.form.get("blacksmithing"),
blacksmithing_spec = request.form.get("blacksmithing_spec"),
commerce = request.form.get("commerce"),
commerce_spec = request.form.get("commerce_spec"),
jeweller = request.form.get("jeweller"),
jeweller_spec = request.form.get("jeweller_spec"),
leather_working = request.form.get("leather_working"),
leather_spec = request.form.get("leather_spec"),
masonry = request.form.get("masonry"),
masonry_spec = request.form.get("masonry_spec"),
metal_working = request.form.get("metal_working"),
metal_working_spec = request.form.get("metal_working_spec"),
sailing = request.form.get("sailing"),
sailing_spec = request.form.get("sailing_spec"),
tailoring = request.form.get("tailoring"),
tailoring_spec = request.form.get("tailoring_spec"),
woodworking = request.form.get("woodworking"),
woodworking_spec = request.form.get("woodworking_spec")
)
db.execute("""INSERT into
cha_gen_skills (
cha_id, acrobatics, acrobatics_spec, culture, culture_spec,
dancing, dancing_spec, first_aid, first_aid_spec,
manual_labour, labour_spec, musical_ability, music_spec,
observation, observation_spec, sleight, sleight_spec,
sneak, sneak_spec, social_interaction, social_spec, tracking,
tracking_spec, upkeep_maintenance, upkeep_spec
) VALUES (
:cha_id, :acrobatics, :acrobatics_spec, :culture, :culture_spec,
:dancing, :dancing_spec, :first_aid, :first_aid_spec,
:manual_labour, :labour_spec, :musical_ability, :music_spec,
:observation, :observation_spec, :sleight, :sleight_spec,
:sneak, :sneak_spec, :social_interaction, :social_spec, :tracking,
:tracking_spec, :upkeep_maintenance, :upkeep_spec
)
""",
acrobatics = request.form.get("acrobatics"),
acrobatics_spec = request.form.get("acrobatics_spec"),
culture = request.form.get("culture"),
culture_spec = request.form.get("culture_spec"),
dancing = request.form.get("dancing"),
dancing_spec = request.form.get("dancing_spec"),
first_aid = request.form.get("first_aid"),
first_aid_spec = request.form.get("first_aid_spec"),
manual_labour = request.form.get("manual_labour"),
labour_spec = request.form.get("labour_spec"),
musical_ability = request.form.get("musical_ability"),
muscic_spec = request.form.get("music_spec"),
observation = request.form.get("observation"),
observation_spec = request.form.get("observation_spec"),
sleight = request.form.get("sleight"),
sleight_spec = request.form.get("sleight_spec"),
sneak = request.form.get("sneak"),
sneak_spec = request.form.get("sneak_spec"),
social_interaction = request.form.get("social_interaction"),
social_spec = request.form.get("social_spec"),
tracking = request.form.get("tracking"),
tracking_spec = request.form.get("tracking_spec"),
upkeep_maintenance = request.form.get("upkeep_maintenance"),
upkeep = request.form.get("upkeep_spec")
)
db.execute("""INSERT into
cha_lang_skills (
cha_id, common, common_spec, dwarven, dwarven_spec, elvish,
elvish_spec, gnomeish, gnomeish_spec, halfling, halfling_spec
) VALUES (
:cha_id, :common, :common_spec, :dwarven, :dwarven_spec, :elvish,
:elvish_spec, :gnomeish, :gnomeish_spec, :halfling, :halfling_spec
)
""",
common = request.form.get("common"),
common_spec = request.form.get("common_spec"),
dwarven = request.form.get("dwarven"),
dwarven_spec = request.form.get("dwarven_spec"),
elvish = request.form.get("elvish"),
elvish_spec = request.form.get("elvish_spec"),
gnomeish = request.form.get("gnomeish"),
gnomeish_spec = request.form.get("gnomeish_spec"),
halfling = request.form.get("halfling_spec")
)
As far as I Understand it, the first section of code creates the cha_id but then how do I call it back for the subsequent calls?
These are the SQL tables...
-- Table structure for character_basics
----
CREATE TABLE 'character_basics' ('char_id' integer PRIMARY KEY AUTOINCREMENT NOT NULL, 'user_id' integer NOT NULL, 'name' text NOT NULL, 'race' text NOT NULL, 'cha_class' text NOT NULL, 'level' integer NOT NULL, 'experience' integer NOT NULL, 'alignment_lc' text NOT NULL, 'alignment_ge' text NOT NULL, 'height' integer NOT NULL, 'weight' integer NOT NULL, 'handedness' text NOT NULL, 'eye_colour' text NOT NULL, 'hair_colour' text NOT NULL, 'sex' text NOT NULL, 'age' integer NOT NULL, 'status' text NOT NULL, 'max_hp' integer NOT NULL, 'current_hp' integer NOT NULL, 'strength' integer NOT NULL, 'dexterity' integer NOT NULL, 'constitution' integer NOT NULL, 'intelligence' integer NOT NULL, 'wisdom' integer NOT NULL, 'charisma' integer NOT NULL, 'fortune' INTEGER NOT NULL );
----
-- Table structure for char_profession
----
CREATE TABLE 'char_profession' ('char_id' integer PRIMARY KEY NOT NULL, 'agriculture' integer NOT NULL, 'agriculture_spec' text, 'blacksmithing' integer NOT NULL, 'blacksmithing_spec' text, 'commerce' integer NOT NULL, 'commerce_spec' text, 'jeweller' integer NOT NULL, 'jeweller_spec' text, 'leather_working' integer NOT NULL, 'leather_spec' text, 'masonry' integer NOT NULL, 'masonry_spec' text, 'metal_working' integer NOT NULL, 'metal_working_spec' text, 'sailing' integer NOT NULL, 'sailing_spec' text, 'tailoring' integer NOT NULL, 'tailoring_spec' text, 'woodworking' INTEGER NOT NULL , 'woodworking_spec' text);
----
-- Table structure for cha_gen_skills
----
CREATE TABLE 'cha_gen_skills' ('char_id' integer NOT NULL, 'acrobatics' integer NOT NULL, 'acrobatics_spec' text, 'culture' integer NOT NULL, 'culture_spec' text, 'dancing' integer NOT NULL, 'dancing_spec' text, 'first_aid' integer NOT NULL, 'first_aid_spec' text, 'manual_labour' integer NOT NULL, 'labour_spec' text, 'musical_ability' integer NOT NULL, 'music_spec' text, 'observation' integer NOT NULL, 'observation_spec' text, 'sleight' integer NOT NULL, 'sleight_spec' text, 'sneak' integer NOT NULL, 'sneak_spec' text, 'social_interaction' integer NOT NULL, 'social_spec' text, 'tracking' integer NOT NULL, 'tracking_spec' text, 'upkeep_maintenance' integer NOT NULL, 'upkeep_spec' text);
----
-- Table structure for char_academic
----
CREATE TABLE 'char_academic' ('char_id' integer PRIMARY KEY NOT NULL, 'anatomy' integer NOT NULL, 'anatomy_spec' text, 'animal_lore' integer NOT NULL, 'animal_lore_spec' text, 'arcana' integer NOT NULL, 'arcana_spec' text, 'botany_forestry' integer NOT NULL, 'botany_spec' text, 'engineering' integer NOT NULL, 'engineering_spec' text, 'history' integer NOT NULL, 'history_spec' text, 'appraisal' INTEGER NOT NULL , 'appraisal_spec' text, 'mathematics' INTEGER NOT NULL , 'mathematic_spec' text, 'religion' INTEGER NOT NULL , 'religion_spec' text);
Thanks
Ed
A customer needs a trigger for his new management tool. It works so far, but now he wants to get values from another table.
What does it mean? Take a short look at the (working) trigger:
CREATE OR REPLACE TRIGGER reldb_export_T_Import3_In
AFTER UPDATE OR INSERT OR DELETE ON infor.RELDB
FOR EACH ROW
DECLARE KST varchar(1000 BYTE);
BEGIN
IF (:new.ZUST = 4 AND :old.ZUST = 3 AND :old.SAINT = 60)
THEN
INSERT INTO infor2infoboard.T_Import3_In
(Idx, Timestamp, ObjectId, Text, RowUid, GroupName, GroupIndex, PredTimeCondition, PredGapValue, Service, ReqQuan, ReqDur,
Efficiency, BackColor, HighlightColor, Brightness, CellOffsetY, Fixed, Font, FontColor, Height, EarliestStart, LastDeliveryDate, RoleId,
StatusIds, WithReservation, LastChange, LastEditedBy, Opacity, IsProcess, CheckSymbol, Link, Priority, IconPath, Adress, CA1, CA2,
CA3, CA4, CA5, CA6, CA7, CA8, CA9, CA10, CA11, CA12, CA13, CA14, CA15,
CA16, CA17, CA18, CA19, CA20, CA21, CA22, CA23, CA24, CA25,
CA26, CA27, CA28, CA29, CA30, CA31, CA32)
VALUES
(seq_t_import3_in.nextval, sysdate, :old.RNR, NULL, CASE WHEN :NEW.KST = '3710' THEN 'Pool TB-M 1;Pool TB-M 2;Pool TB-M 3;Pool TB-M 4;Pool TB-M 5' ELSE KST END, :old.Komm, NULL, NULL, NULL, :old.KTXT, :old.TA_4, NULL,
NULL, CASE WHEN :new.MNR = '3740' THEN 'GREEN' WHEN :new.MNR != '3740' THEN 'BLUE' END, NULL, NULL, NULL, NULL, NULL, NULL, NULL, :old.TERM_2, :old.TERM_1, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, SUBSTR(:old.ANR, 1, 10), :old.KOMM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
END IF;......
Now he want to replace one of the last NULLS with a selected value.
The statement should look like this:
SELECT KTXT FROM RELDB WHERE ANR = :old.ANR and SAEXT = 'H';
So, for example:
NULL, (SELECT KTXT FROM RELDB WHERE ANR = :old.ANR and SAEXT = 'H';), SUBSTR(:old.ANR, 1, 10), :old.KOMM, NULL
My problem: I'm getting an error, that the trigger couldn't notice some changes. The trigger won't get fired.
So does anyone know how I can use or how I can get selected values (from the same (!!!) table which the trigger is listening to?
I'd appreciate your help :)
Cheers,
Dom
In Oracle, you cannot select from the same table you are changing, this is the mutating table error. See https://asktom.oracle.com/pls/apex/f?p=100:11:0::::p11_question_id:9579487119866
I'm trying to update my user table, which my code does. but for some reason it keeps executing the else statement.
in the documentation it states that updateByPk should return the number of rows being updated. Which should be 1. what am i missing here? and how do i check if the table has been updated successfully?
if (User::model()->updateByPk($model->id, array("last_login"=> Shared::timeNow())))
{
Yii::app()->user->login($identity, $duration);
echo json_encode(array('error' => false, 'success' => url(app()->users->getHomeUrl())));
Yii::app()->end();
}
else {
echo json_encode(array('error' => 'Could not update user info', 'code' => 'auth'));
Yii::app()->end();
}
my table schema is this
CREATE TABLE IF NOT EXISTS `user` (
`id` int(10) unsigned NOT NULL,
`username` varchar(100) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`gender` char(1) DEFAULT NULL,
`birthday` date DEFAULT '0000-00-00',
`address` varchar(255) DEFAULT NULL,
`city` varchar(45) DEFAULT NULL,
`state` varchar(45) DEFAULT NULL,
`website` text,
`postal_code` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
`password` varchar(63) DEFAULT NULL,
`activate` varchar(63) NOT NULL DEFAULT '1',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_login` datetime DEFAULT NULL,
`password_reset` int(11) unsigned DEFAULT NULL,
`admin` tinyint(1) unsigned NOT NULL DEFAULT '0',
`email_verified` tinyint(1) unsigned DEFAULT NULL,
`login_disabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
`oauth_id` bigint(20) DEFAULT NULL,
`oauth_username` varchar(255) DEFAULT NULL,
`oauth_provider` varchar(10) DEFAULT NULL,
`oauth_email` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
You should check $model exits first otherwise updateByPk can return 0
if ($model && User::model()->updateByPk($model->id, array("last_login"=> Shared::timeNow())))
Your table does not specify a primary key. Yet you update by primary key.
You have a few choices.
Change your table structure to include a primary key
Use another update method. For example, Update() allow you to pass a criteria object.
Know it's old, but : https://www.php.net/manual/en/pdostatement.rowcount.php
If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
And Yii1 use rowCount
Model can be not updated because last_login attribute has not valid value.
Try get model errors ($model->errors()) or check model by $model->validate();
Why you not implement this as follows:
$model->last_login = Shared::timeNow();
if ($model->save())
{
Yii::app()->user->login($identity, $duration);
echo json_encode(array('error' => false, 'success' => url(app()->users->getHomeUrl())));
Yii::app()->end();
}
else {
echo json_encode(array('error' => 'Could not update user info', 'code' => 'auth'));
Yii::app()->end();
}