CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Directly querying Civi data with SQL
Pages: [1]

Author Topic: Directly querying Civi data with SQL  (Read 2001 times)

RicardoDavis

  • I’m new here
  • *
  • Posts: 29
  • Karma: 0
  • CiviCRM version: 4 (yay!)
  • CMS version: 7
  • MySQL version: 5.1
  • PHP version: 5.2
Directly querying Civi data with SQL
June 14, 2011, 11:18:46 am
Hello all,

I'm a CiviCRM power user with developer experience in other CRM systems (especially NetSuite). My background is in database analysis, design, and development.  I'm now working for a not-for-profit that's using CiviCRM 3.1 (the upgrade is in the planning stages) that is having problems with their data from a less than satisfactory conversion from 2.2 to 3.1.  In particular the household and individual contacts need some corrective work.

My first thought was to just fire up MySQLWorkbench, connect to a copy of the database and begin work.  I reverse-engineered the schema so I can get an idea of how the data is structured.  The first problem I checked is the household_name and display_name values in household contacts, so I created the query to find problems with a specific contact:

SELECT id, contact_type, household_name, display_name, first_name, middle_name, last_name
FROM civicrm_contact
WHERE display_name LIKE "%smithe%" AND display_name "%john%";

Then in the Search Builder I created a search to do the same by selecting the record type "Household" and using the same criteria as above.

Name or Email LIKE - '%smithe%' ...AND...
 Name or Email LIKE - '%john%' ...AND...
 Contact Type - 'Household'

The three problem contacts show up in both result sets, however I noticed that the following.

* In the SQL query results the value of contact_type is "Individual" rather than the expected "Household".  In fact when perform a SQL query to see how many records there are for each contact_type there is only one household but when I use the Search Builder it finds over 250K contacts of type household. Why? 
* It was obvious by inspecting the household details in Civi contact page that the civicrm_contact.id field is the External ID that shows up in details of the Search Results page.  But I don't see what field in the table is related to the "CiviCRM ID" on the Search Results.  Where is it?

The question you may be thinking is, "why doesn't he write PHP code to do the queries?"  The quick answer is - why do I have to use another tool to get to the data when the data is stored in MySQL? 

-Ricardo

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Directly querying Civi data with SQL
June 14, 2011, 01:00:00 pm
civicrm_contact.id is the ID you see on search results (it's the primary key for each contact record).

These links may help you make progress:
http://wiki.civicrm.org/confluence/display/CRMDOC40/Ensuring+Schema+Integrity+on+Upgrades
http://wiki.civicrm.org/confluence/display/CRMDOC40/Database+Troubleshooting+Tools
http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+ERD+3.3
Protect your investment in CiviCRM by  becoming a Member!

RicardoDavis

  • I’m new here
  • *
  • Posts: 29
  • Karma: 0
  • CiviCRM version: 4 (yay!)
  • CMS version: 7
  • MySQL version: 5.1
  • PHP version: 5.2
Re: Directly querying Civi data with SQL
June 15, 2011, 07:48:10 am
Thanks Dave; I had found CiviCRM DB Tools earlier that afternoon so I'm glad you referred it.  I read through the documentation and found that it was meant to be used up to CiviCRM 3.0, so I kept this in mind as I plowed forward.

I ran the integrity check and the first thing I noticed was:

   Got version 2.2.0 from civicrm_domain.version

I don't think this is good news if the conversion was supposed to bring the database to version 3.1.  As you might imagine there were screens of warnings and errors as the integrity check scanned the database.  I'm going to need to review the reference 3.1 schema to compare the schema errors/warnings.

I'll probably try manually updated the version value just to see how the integrity check responds., but if the guy to supposedly did this conversion missed something as fundamental as changing the domain version then it looks like I have a lot of work ahead of me.

This might explain some of the anomalies I'm seeing when I query the database directly versus using the Search Builder, as well as some of the other quirks that the staff has related to me.

Once I've verified the extent of the problems, then I'll review at my options to move forward.  Any suggestions if what the integrity check reported is mostly true?

-R

RicardoDavis

  • I’m new here
  • *
  • Posts: 29
  • Karma: 0
  • CiviCRM version: 4 (yay!)
  • CMS version: 7
  • MySQL version: 5.1
  • PHP version: 5.2
Re: Directly querying Civi data with SQL
June 16, 2011, 09:48:59 am
Yesterday I downloaded the CiviCRM 3.1.5 tarball and used the SQL scripts to build a clean database (clean_civ3).  I then compared the schema to the production database (prod_civ3).  Starting with the civicrm_contact table I see some significant differences.  I also see tables in clean_civ3 that aren't in prod_civ3.  So my hunch that prod_civ3 isn't the schema for CiviCRM 3.1.5 was right, although that's the version of the app that's running now.  I also noticed in the CiviCRM 3.1.5 release notes that one could upgrade from CiviCRM 2.2 but it wasn't explicitly stated whether one had the option to use the old schema.  Is it possible or advisable to do this?  The guy who performed the upgrade was able to do something like this, and it might explain why I'm not seeing the household contacts that I noted in my original post even though CiviCRM has the data.

Right now it looks like I'm going to have to export their data and rebuild it in a clean install of 3.4 or 4.0.  I don't like the idea, but I don't see any better options.

On a related note, is there a compelling reason to upgrade to CiviCRM 4.0 now?

Thanks again,

-Ricardo



ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: Directly querying Civi data with SQL
June 16, 2011, 08:52:48 pm
Ricardo,

The CiviFolks have announced they don't plan to develop 3.x beyond 3.4 (though there is a make-it-happen initiative to extend that).

Ken

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Directly querying Civi data with SQL
June 17, 2011, 07:31:07 am
Quote from: RicardoDavis on June 16, 2011, 09:48:59 am
On a related note, is there a compelling reason to upgrade to CiviCRM 4.0 now?

Yes, that's definitely recommended. As noted above and also fr ongoing CMS support.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Directly querying Civi data with SQL

This forum was archived on 2017-11-26.