AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Mysql Typs (Null) Typ (Integer)

Ein Thema von question · begonnen am 25. Aug 2013 · letzter Beitrag vom 25. Aug 2013
Antwort Antwort
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#1

Mysql Typs (Null) Typ (Integer)

  Alt 25. Aug 2013, 19:27
Hello,
I am using Delphi with mysql, i have a table which contains the data.For example
Table A has ID,Name,Phone and so on , the empty field are filled with "Null"
ID=1, Name=To,Phone=null,

I would like to copy the data from Table A to Table B, but the problem is, if there is no value in a filed then its filled with '0' instead of Null and i got a error messege

"Variante des Typs (Null) konnte nicht in Typ (Integer) konvertiert werden."

ID=1,Name=Tom,Phone=0

my question is, why one table put Null and others 0,since both are same field and same type

need suggestion, i wanna filled the emtpy field not with 0 rather Null,how to do it?

Code:
Query.SQL.Clear;
    Query.SQL.Add('SELECT * FROM TableA WHERE ID = :ID');
    Query.ParamByName('ID').AsInteger := NewID;
    Query.Open;

    while (Query.Eof) and (Query.RecordCount > 0) do

   begin
      QueryB.Close;
      QueryB.SQL.Clear;

      QueryB.SQL.Add('INSERT INTO `TableB` (`ParentID`,`NewID`, and so on
     ,.......
      QueryB.ParamByName('ParentID').AsInteger := Query.FieldByName('ParentID').AsInteger;
      QueryB.ParamByName('NewID').AsInteger := NewID;
I would like to fill the empty field with "null" instad of "0" what should i change ?
  Mit Zitat antworten Zitat
Benutzerbild von sx2008
sx2008

Registriert seit: 15. Feb 2008
Ort: Baden-Württemberg
2.332 Beiträge
 
Delphi 2007 Professional
 
#2

AW: Mysql Typs (Null) Typ (Integer)

  Alt 25. Aug 2013, 20:43
Delphi-Quellcode:
// Fill a parameter with NULL
QueryB.ParamByName('NewID').Value := Null;

// Fill a field with NULL
QueryB['SomeField'] := Null;
// alternative way to set a field to NULL
QueryB.FieldByName('SomeField').Clear;

// testing if a field is NULL
if Query.FieldByName('SomeField').IsNull then ...



// Copy a Field that might be NULL
// the VCL converts different datatypes on the fly e.g. from Integer -> string
QueryB['DestinationField'] := QueryA['SourceField'];
// this is the same as (long version of the line above)
QueryB.FieldByName('DestinationField').Value := QueryA.FieldByName('SourceField').Value;



// and some bonus info...
// how to set the SQL string of a query in a single line without using .Clear() followed by a .Add()
Query.SQL.Text := 'SELECT * FROM TableA WHERE ID = :ID';
fork me on Github
  Mit Zitat antworten Zitat
question

Registriert seit: 17. Apr 2013
97 Beiträge
 
#3

AW: Mysql Typs (Null) Typ (Integer)

  Alt 25. Aug 2013, 21:17
Code:
QueryA.ParamByName('ID').AsInteger := QueryBID.AsInteger //this using the field of VCL Component)
QueryYA.ParamByName('ID').AsInteger := ueryB.FieldByName(('ID').AsInteger;// this using sqil query fieldbyname
value of both query can be different, i mean Null and 0?
  Mit Zitat antworten Zitat
Benutzerbild von Valle
Valle

Registriert seit: 26. Dez 2005
Ort: Karlsruhe
1.223 Beiträge
 
#4

AW: Mysql Typs (Null) Typ (Integer)

  Alt 25. Aug 2013, 22:22
You can copy data without using Delphi:

Code:
-- untested, might need some more parenthesis
INSERT INTO `B` (foo, bar)
SELECT foo, bar FROM `A` WHERE id = 42;
That requires less network traffic and should be much faster.

Maybe that would be a better alternative for you.
Valentin Voigt
BOFH excuse #423: „It's not RFC-822 compliant.“
Mein total langweiliger Blog
  Mit Zitat antworten Zitat
Benutzerbild von sx2008
sx2008

Registriert seit: 15. Feb 2008
Ort: Baden-Württemberg
2.332 Beiträge
 
Delphi 2007 Professional
 
#5

AW: Mysql Typs (Null) Typ (Integer)

  Alt 25. Aug 2013, 22:41
Code:
QueryA.ParamByName('ID').AsInteger := QueryBID.AsInteger //this using the field of VCL Component)
QueryYA.ParamByName('ID').AsInteger := ueryB.FieldByName(('ID').AsInteger;// this using sqil query fieldbyname
If you want to transfer a Value that could be both a integer or NULL you have to use the data type "Variant".
A Variant can contain different data type like integer, floats, booleans, strings.
And a Variant can also be Null !
This means that Variant is a nullable data type.
To get or set the value of a field you can use the property Value .
The Properties AsString, AsInteger, AsBoolean,... are specialised properties.

Delphi-Quellcode:
// your code example using property Value instead of property AsInteger
QueryA.ParamByName('ID').Value := QueryBID.Value; //this using the field of VCL Component)
QueryYA.ParamByName('ID').Value := ueryB.FieldByName(('ID').Value;// this using sqil query fieldbyname
fork me on Github
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:31 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz