Last active
October 20, 2020 08:49
-
-
Save reuniware/ef0f0f0a59fe1a2f98ef4e7fb9ff1039 to your computer and use it in GitHub Desktop.
Windev Mobile 25 : SQLite procédures Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.database.Cursor; | |
public static void AjouteArticle(String codeArticle, double quantite) | |
{ | |
//Context context = getActiviteEnCours(); | |
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("superdb", null); | |
db.execSQL("CREATE TABLE IF NOT EXISTS article(code TEXT, quantite REAL);"); | |
db.execSQL("INSERT INTO article (code, quantite) VALUES ('" + codeArticle + "', " + quantite + ");"); | |
db.close(); | |
} | |
public static int CompteArticles() | |
{ | |
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("scandb", null); | |
int count = 0; | |
if (TableArticleExiste() == true) { | |
Cursor mCount= db.rawQuery("select count(*) from article;", null); | |
mCount.moveToFirst(); | |
count = mCount.getInt(0); | |
mCount.close(); | |
} | |
db.close(); | |
return count; | |
} | |
public static void MajArticle(String rowId, double quantite) | |
{ | |
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("scandb", null); | |
db.execSQL("UPDATE article SET quantite=" + quantite + " WHERE rowId=" + rowId + ";"); | |
db.close(); | |
} | |
public static String ObtientArticle(int indexArticle) | |
{ | |
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("superdb", null); | |
Cursor mCount= db.rawQuery("select * from article;", null); | |
mCount.moveToFirst(); | |
for(int i=0;i<indexArticle;i++) { | |
mCount.moveToNext(); | |
} | |
String codeArticle = mCount.getString(0); | |
float quantite = mCount.getFloat(1); | |
mCount.close(); | |
db.close(); | |
return codeArticle + "\t" + String.valueOf(quantite); | |
} | |
public static void SupprimerArticles() | |
{ | |
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("superdb", null); | |
db.execSQL("delete from article;"); | |
db.close(); | |
return; | |
} | |
public static boolean TableArticleExiste() | |
{ | |
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("scandb", null); | |
Cursor mCount= db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='article';", null); | |
if (mCount.getCount() == 0){ | |
db.close(); | |
return false; | |
} else { | |
db.close(); | |
return true; | |
} | |
} | |
// Exemples d'utilisations (ajout, compter, récupérer, vider la table) | |
//AJOUT | |
//pour i = 1 a 3000 | |
// AjouteArticle("0000111122", 2.1) | |
//FIN | |
//COMPTER | |
//gnNbArticles est un entier = CompteArticles() | |
//Info(gnNbArticles) | |
//OBTENIR | |
//gsValretour est une chaine = ObtientArticle(CompteArticles()-1) | |
//gsCodearticle est une chaine = ExtraitChaîne(gsValretour, 1, TAB) | |
//grQuantite est un réel = Val(ExtraitChaine(gsValretour, 2, TAB)) | |
//Info("code=" + gsCodearticle + " qté=" + grQuantite) | |
//VIDER | |
//SupprimerArticles() | |
//Liens utiles | |
//http://gkonandroid.blogspot.com/2014/01/sqlitedatabase-without-sqliteopenhelper.html | |
//https://www.sqlitetutorial.net/sqlite-unique-constraint/ | |
//https://www.programcreek.com/java-api-examples/?class=android.database.sqlite.SQLiteDatabase&method=openOrCreateDatabase | |
//https://codesnippets.fesslersoft.de/check-if-a-table-exists-in-sqlite/#:~:text=To%20check%20if%20a%20Table%20exists%20in%20SQLite,SELECT%20name%20FROM%20sqlite_master%20WHERE%20type%3D%27table%27%20AND%20name%3D%27TableName%27%3B | |
//https://stackoverflow.com/questions/37834996/how-to-check-whether-a-table-exist-in-android-sqlite#:~:text=You%20can%20use%20the%20following%20statement%20to%20check,information%20of%20all%20the%20tables%20in%20the%20database. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment