BOM Database Option

How about extending the database support to store BOMs? I’ve been doing this for years with Orcad and Viewdraw via scripts and MySQL. I export refdes per row and index by assembly and assembly rev. It is quite easy to diff revisions to see changes. SQL is an excellent language to process PCB type data. I also use SQL queries to verify our MRP BOMs against against schematic design BOMs.

1 Like

Du könntest auch eine (ungruppierte) Stückliste exportieren als CSV und diese dann in Python einlesen und in eine Datenbank speichern.

Vielleicht gibt dir folgendes Pythonscript eine Idee:

#!/usr/bin/env python3

import argparse
import csv
import sqlite3
import sys


###########################
#Parse die Argumentenliste#
###########################
kommandozeile=argparse.ArgumentParser()
kommandozeile.add_argument\
  (
    "--stueckliste",
    action="store",
    type=str,required=True,
    dest="stuecklistendateiname",
    help="Pfad für die von KiCad expotierte Stückliste. Wichtig ist das die Gruppierung"
         " augeschalttet wurde, damit die Bauteile 1:1 in die Datenbank geladen werden können."
  )

kommandozeile.add_argument\
  (
    "--datenbank",
    action="store",
    type=str,required=True,
    dest="datenbankdateiname",
    help="Pfad für die sqlite3 Datenbank in der die Stückliste gespeichert werden sollte."
  )

kommandozeile.add_argument\
  (
    "--feldnamenpraefix",
    action="store",
    type=str,required=False,
    default="feld_",
    dest="feldnamenpraefix",
    help="Pfad für die sqlite3 Datenbank in der die Stückliste gespeichert werden sollte."
  )

argumente=kommandozeile.parse_args(sys.argv[1:])

datenbankverbindung=sqlite3.connect(argumente.datenbankdateiname)
datenbankmarker=datenbankverbindung.cursor()



##################################################
#Bestimme die namen der Kollonen in der Datenbank#
##################################################
datenbankmarker.execute("SELECT * FROM stuecklisteneintrag LIMIT 1")
kolonnen=[]
for i in datenbankmarker.description:
  kolonnenname = i[0]
  if kolonnenname.startswith(argumente.feldnamenpraefix):
    if '`' in kolonnenname:
      # Wir können die Kolonnennamen nicht wie normal mit einem ? einfügen sondern müssen diese
      #  direkt als string einfügen.
      raise Exception("Kann nicht mit Kollonnen umgehen die ein ` enthalten")
    kolonnen.append( kolonnenname[len(argumente.feldnamenpraefix):] )



#############################
#Mache die Datenbankeinträge#
#############################
def erstelleSqlliste(n):
  return ', '.join(['?'] * n)

with open(argumente.stuecklistendateiname, encoding='utf-8') as stuecklistendatei:
  parser = csv.reader(stuecklistendatei)
  spalten = parser.__next__()

  verwendeteSpalten=[]
  verwendeteKolonnen=[]
  for spaltennummer,spalte in enumerate(spalten):
    if spalte in kolonnen:
      verwendeteSpalten.append(spaltennummer)
      verwendeteKolonnen.append('`'+argumente.feldnamenpraefix+spalte+'`')

  verwendeteKolonnen = ', '.join(verwendeteKolonnen)

  datenbankmarker.execute\
    ("INSERT INTO stueckliste ( stuecklistendateiname ) VALUES ( ? ) ", [argumente.stuecklistendateiname] )
  stuecklistenid = datenbankmarker.lastrowid

  l = erstelleSqlliste(len(verwendeteSpalten))
  for zeile in parser:
    spaltenwerte = [ zeile[i] for i in verwendeteSpalten ]
      
    datenbankmarker.execute\
      (
        "INSERT INTO stuecklisteneintrag"
        " ( stueckliste, " + verwendeteKolonnen + " )"
        " VALUES ( ?, " + l + " ) ",
        [stuecklistenid]+spaltenwerte 
      )

datenbankverbindung.commit()

Für diese Datenbank:

CREATE TABLE `stueckliste`
  (
    -- MySQL führt den Code in /*!40101 AUTO_INCREMENT */ aus (Sofern MySQL Version >=4.1.1)
    -- Damit kann code geschrieben werden der nur für MySQL gilt und nicht für sqlite3
    -- MySQL braucht das AUTO_INCREMENT, sqlite3 funktioniert nur ohne AUTO_INCREMENT
    `id`                     INTEGER PRIMARY KEY /*!40101 AUTO_INCREMENT */,

    `stuecklistendateiname` TEXT             NOT NULL,
    `erstellungszeitpunkt`  TEXT             NOT NULL    DEFAULT CURRENT_TIMESTAMP
  );


CREATE TABLE `stuecklisteneintrag`
  (
    `id`                     INTEGER PRIMARY KEY /*!40101 AUTO_INCREMENT */,

    -- Zeiger zu der Tabelle stueckliste
    stueckliste              INTEGER          NOT NULL,

    `feld_wert`              TEXT                         DEFAULT NULL,
    `feld_artikel`           TEXT                         DEFAULT NULL,
    `feld_beschreibung`      TEXT                         DEFAULT NULL,
    -- Hier können noch mehr Felder hinzugefügt werden.

    FOREIGN KEY(stueckliste) REFERENCES stueckliste(id)
  );

Du müsstest das wahrscheinlich anpassen auf deine Tabellenstruktur und es von sqlite3 auf MySQL konvertieren. Aber so schwer sollte das nicht sein.

I remind that the language of this international forum is English. Even if two parties know some other language, they have to write in English to benefit all readers.

Should i delete my posts?

they have to write in English to benefit all readers.

I don’t see how that works. That would require all readers know English.

This is an English-language KiCad forum, so, yes…

There are some others in other languages linked here, but currently none in German: Forums | KiCad EDA

I think switching languages to help communicate to someone who does not speak English well is OK, but when doing so, it is polite to also provide an English translation.

1 Like

The bottom line is we help where we can. The biggest problem is keeping tabs on whether non-English posts are spam or disrespectful. The moderators rely on community help in that regard. If I personally see a non-English thread and a respected poster is involved, that’s generally good enough for me to not worry about it.

I’m pretty sure @ChrisGammell has weighed in on this subject before.

Now, if I plug that code into my English-centric system, will it puke? :wink:

I shoved this though Google translate as I was interested in the topic.

You could also export a (ungrouped) parts list as CSV and then read it in Python and save it to a database.

Maybe the following Python script will give you an idea

#! /Usr/bin/env python3

Import argparse

Import csv

Import sqlite3

Import sys

###########################

#Parse the list of arguments#

###########################

Command line=argparse.ArgumentParser()

Command line.add_argument\

(

“–List of pieces”,

Action=“store”,

type=str,required=True,

Dest=“list file name”,

help=“Path for the parts list exposed by KiCad. The important thing is the grouping”

" was switched on so that the components can be loaded into the database 1:1."

)

Command line.add_argument\

(

“–Database”,

Action=“store”,

type=str,required=True,

Dest=“database file name”,

help=“Path to the sqlite3 database in which the parts list should be stored.”

)

Command line.add_argument\

(

“–Field name praefix”,

Action=“store”,

type=str,required=False,

Default=“field_”,

Dest=“field name praefix”,

help=“Path to the sqlite3 database in which the parts list should be stored.”

)

Argumente=commandozeile.parse_args(sys.argv[1:])

Database connection=sqlite3.connect(arguments.database file name)

Database marker=database connection.cursor()

##################################################

#Determine the names of the clones in the database#

##################################################

Database marker.execute(“SELECT * FROM playlist entry LIMIT 1”)

Columns=[]

For i in datenbankmarker.description:

Column name = i[0]

If columnname.startswith(arguments.fieldnamepraefix):

If ‘`’ in column name:

We can’t use the column names like normal with one? But must insert these

insert directly as a string.

Raise Exception(“Can’t handle columns that contain a `”)

Column.append( column name[len(argument.field namepraefix):] )

###############################

#Make the database entries#

###############################

Def create SQLlist(s):

Return ‘, ‘.join([’?’] * n)

With open(argumente.stu-list file name, encoding=‘utf-8’) as list file:

Parser = csv.reader(list file)

Columns = parser.next()

Columns used=[]

Used columns=[]

For column number, column in enumerate(columns):

If split into columns:

Used columns.append(column number)

Used columns.append(‘'+arguments.field namespraefix+column+'’)

Used columns = ', '.join(used columns)

Database marker.execute\

("INSERT INTO playlist ( playlist file name ) VALUES ( ? ) ", [arguments.list file name] )

Stuecklistenid = database marker.lastrowid

L = create Sqllist(len(used columns))

For line in parser:

Column values = [ line[i] for i in used columns ]

Database marker.execute\

(

“INSERT INTO playlist entry”

" (list, " + used columns + " )"

" VALUES ( ?, " + l + " ) ",

[Stu-listid]+column values

)

Database connection.commit()

You would probably have to adapt this to your table structure and convert it from sqlite3 to MySQL. But it shouldn’t be that hard.

There are other reasons too. A lot of people just search this forum for answers. Some don’t even create an account. Threads in other languages are not searchable (for english words).

And even if people who search the forum find the right thread. It is quite possible they won’t understand what is written. Apart from English, I’ve seen German, French, Russian and even some Chinese on this forum, and I can’t do much with any of those languages.

No, I don’t think you should delete your posts - I think we can all value contributions from speakers of many languages. I would just suggest that you add a translation.
Rightly or wrongly , English considered the lingua franca here. I have conversational French, a little Spanish, less German, cafe Italian and barely get by in Romanian, Greek and Polish. I try hard but ultimately for any meaningful conversation it probably needs to be in a language that a majority can contribute to and that is probably English. As a native (British) anglophone I am naturally, somewhat embarrassed by this but there are more non-native English speakers than native anglophones.
Google - and others- provide acceptable translations - even if not grammatically perfect.

Apparently there are 7000 spoken languages in the world. Worth adding a translation to get contributions from the wide range of experience here from the speakers of the approx 6999 non-English languages in the world.

I first opened this thread yesterday. There was one reply to the OP in German. I was a little surprised so I clicked the OPs avatar to read that the OP resided in the US.

My first thoughts were “I wonder if the OP could understand the reply?” as the OPs post read as a typical native English speaker.
Next thoughts were: “Johannespfister will realise his mistake, swap keyboard languages and edit his reply into English.”

This is a great comment… I even gave it a “Like” :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.