N
N
nikita_chiru2016-05-25 11:11:17
Java
nikita_chiru, 2016-05-25 11:11:17

How to compare strings in a database?

How to compare rows in a database?
and how to write, if there is already a line with the given text, then do nothing, and if there are no matches, create a new line
public class DBHelper extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contactDb";
public static final String TABLE_CONTACTS = "contacts";
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_MAIL = "mail";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_CONTACTS + "(" + KEY_ID
+ " integer primary key," + KEY_NAME + " text," + KEY_MAIL + " text" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_CONTACTS);
onCreate(db);
}
}

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
Max, 2016-05-25
@mbelskiy

Add the UNIQUE keyword for the column and insert all the time. If such a line already exists, the code will simply return an error.

S
sergiu3dmd, 2016-05-26
@sergiu3dmd

db.insertWithOnConflict(TABLE_NAME, null, cv, SQLiteDatabase.CONFLICT_REPLACE);

V
Vladimir Martyanov, 2015-08-18
@vilgeforce

C/C++ if you need native code. Python can still be used, but an interpreter is needed, although Linux usually has one.

S
sim3x, 2015-08-18
@sim3x

bash
Python
Rust
GoLang
Ruby
C

G
globuser, 2015-08-18
@globuzer

Development in C\C++ using the gcc compiler makes it possible to use applications on both nix and win systems, with the exception of minimal modifications.
The Python interpreter (by default it comes in many nix systems), and in WIN systems installed on top - make it possible to write code anywhere and use it virtually without restrictions.
Generally speaking, answering the question "is it possible to write programs for Linux under Windows" - you can safely answer - you can, moreover, you can write in any language: open a notepad in Windows, write code, compile this file already in Linux, transferring it there, to corresponding code page (encoding).
ps Well, the shell (a priori bash) has not been canceled, linux is a powerful tool!

A
Andrey Pavlenko, 2015-08-18
@Akdmeh

It seems to me that your choice is Python, if you need portability and the ability to create under Windows, well, or Java, if the program is more complex.
You can also C++

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question